• Skip to content
  • Skip to link menu
  • KDE API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • KTp
  • Models
contacts-model.cpp
Go to the documentation of this file.
1 /*
2  * Model of all accounts with inbuilt grouping and filtering
3  *
4  * Copyright (C) 2013 David Edmundson <kde@davidedmundson.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "contacts-model.h"
22 
23 #include "contacts-list-model.h"
24 #include "accounts-tree-proxy-model.h"
25 #include "groups-tree-proxy-model.h"
26 #include "text-channel-watcher-proxy-model.h"
27 
28 #include <TelepathyQt/ClientRegistrar>
29 
30 namespace KTp
31 {
32 class ContactsModel::Private
33 {
34 public:
35  GroupMode groupMode;
36  bool trackUnread;
37  QWeakPointer<KTp::AbstractGroupingProxyModel> proxy;
38  KTp::ContactsListModel *source;
39  Tp::AccountManagerPtr accountManager;
40  Tp::ClientRegistrarPtr clientRegistrar;
41  Tp::SharedPtr<KTp::TextChannelWatcherProxyModel> channelWatcherProxy;
42 };
43 }
44 
45 
46 KTp::ContactsModel::ContactsModel(QObject *parent)
47  : KTp::ContactsFilterModel(parent),
48  d(new Private)
49 {
50  d->groupMode = NoGrouping;
51  d->trackUnread = false;
52  d->source = new KTp::ContactsListModel(this);
53 }
54 
55 KTp::ContactsModel::~ContactsModel()
56 {
57  delete d;
58 }
59 
60 
61 void KTp::ContactsModel::setAccountManager(const Tp::AccountManagerPtr &accountManager)
62 {
63  d->accountManager = accountManager;
64 
65  updateGroupProxyModels();
66 
67  //set the account manager after we've reloaded the groups so that we don't send a list to the view, only to replace it with a grouped tree
68  d->source->setAccountManager(accountManager);
69 }
70 
71 Tp::AccountManagerPtr KTp::ContactsModel::accountManager() const
72 {
73  return d->accountManager;
74 }
75 
76 void KTp::ContactsModel::setGroupMode(KTp::ContactsModel::GroupMode mode)
77 {
78  if (mode == d->groupMode) {
79  //if nothing has changed, do nothing.
80  return;
81  }
82 
83  d->groupMode = mode;
84 
85  updateGroupProxyModels();
86 
87  Q_EMIT groupModeChanged();
88 }
89 
90 KTp::ContactsModel::GroupMode KTp::ContactsModel::groupMode() const
91 {
92  return d->groupMode;
93 }
94 
95 void KTp::ContactsModel::setTrackUnreadMessages(bool trackUnread)
96 {
97  if (d->trackUnread == trackUnread) {
98  return;
99  }
100  d->trackUnread = trackUnread;
101 
102  updateGroupProxyModels();
103 
104  Q_EMIT trackUnreadMessagesChanged();
105 }
106 
107 bool KTp::ContactsModel::trackUnreadMessages() const
108 {
109  return d->trackUnread;
110 }
111 
112 void KTp::ContactsModel::updateGroupProxyModels()
113 {
114  //if there no account manager there's not a lot point doing anything
115  if (!d->accountManager) {
116  return;
117  }
118 
119  //if needed set up the client registrar and observer proxy model
120  if (d->trackUnread && d->clientRegistrar.isNull()) {
121  d->clientRegistrar = Tp::ClientRegistrar::create(d->accountManager);
122  d->channelWatcherProxy = Tp::SharedPtr<KTp::TextChannelWatcherProxyModel>(new TextChannelWatcherProxyModel());
123  d->channelWatcherProxy->setSourceModel(d->source);
124  d->clientRegistrar->registerClient(Tp::AbstractClientPtr::dynamicCast(d->channelWatcherProxy), QLatin1String("ListWatcher"));
125  } else if (!d->trackUnread) {
126  //delete the client registrar
127  d->clientRegistrar.reset();
128  d->channelWatcherProxy.reset();
129  }
130 
131  QAbstractItemModel *modelToGroup = 0;
132  if (d->trackUnread) {
133  modelToGroup = d->channelWatcherProxy.data();
134  } else {
135  modelToGroup = d->source;
136  }
137 
138  //delete any previous proxy
139  if (d->proxy) {
140  d->proxy.data()->deleteLater();
141  }
142 
143  switch (d->groupMode) {
144  case NoGrouping:
145  setSourceModel(modelToGroup);
146  break;
147  case AccountGrouping:
148  d->proxy = new KTp::AccountsTreeProxyModel(modelToGroup, d->accountManager);
149  setSourceModel(d->proxy.data());
150  break;
151  case GroupGrouping:
152  d->proxy = new KTp::GroupsTreeProxyModel(modelToGroup);
153  setSourceModel(d->proxy.data());
154  break;
155  }
156 }
157 
158 void KTp::ContactsModel::setSourceModel(QAbstractItemModel *sourceModel)
159 {
160  KTp::ContactsFilterModel::setSourceModel(sourceModel);
161 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal