1 /*
2 * resourcemenu.cpp - helper class for displaying contact's resources
3 * Copyright (C) 2006-2010 Michail Pishchagin
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 */
20
21 #include "resourcemenu.h"
22
23 #include "psiiconset.h"
24 #include "userlist.h"
25 #include "xmpp_status.h"
26 #include "psicontact.h"
27 #include "psiaccount.h"
28
29 /**
30 * \class ResourceMenu
31 * Helper class that displays available resources using QMenu.
32 */
33
ResourceMenu(QWidget * parent)34 ResourceMenu::ResourceMenu(QWidget *parent)
35 : QMenu(parent)
36 , activeChatsMode_(false)
37 {
38 }
39
ResourceMenu(const QString & title,PsiContact * contact,QWidget * parent)40 ResourceMenu::ResourceMenu(const QString& title, PsiContact* contact, QWidget* parent)
41 : QMenu(parent)
42 , contact_(contact)
43 , activeChatsMode_(false)
44 {
45 setTitle(title);
46
47 Q_ASSERT(contact);
48 connect(contact_, SIGNAL(updated()), SLOT(contactUpdated()));
49 contactUpdated();
50 }
51
52 /**
53 * Helper function to add resource to the menu.
54 */
addResource(const UserResource & r)55 void ResourceMenu::addResource(const UserResource &r)
56 {
57 addResource(r.status().type(), r.name());
58 }
59
60 /**
61 * Helper function to add resource to the menu.
62 */
addResource(int status,QString name)63 void ResourceMenu::addResource(int status, QString name)
64 {
65 QString rname = name;
66 if(rname.isEmpty())
67 rname = tr("[blank]");
68
69 //rname += " (" + status2txt(status) + ")";
70
71 QAction* action = new QAction(PsiIconset::instance()->status(status).icon(), rname, this);
72 addAction(action);
73 action->setProperty("resource", QVariant(name));
74 #if defined (Q_OS_MAC) && defined (HAVE_QT5)
75 action->setIconVisibleInMenu(true);
76 #endif
77 connect(action, SIGNAL(triggered()), SLOT(actionActivated()));
78 }
79
actionActivated()80 void ResourceMenu::actionActivated()
81 {
82 QAction* action = static_cast<QAction*>(sender());
83 emit resourceActivated(action->property("resource").toString());
84
85 if (contact_) {
86 XMPP::Jid jid(contact_->jid());
87 jid = jid.withResource(action->property("resource").toString());
88 emit resourceActivated(contact_, jid);
89 }
90 }
91
contactUpdated()92 void ResourceMenu::contactUpdated()
93 {
94 if (!contact_)
95 return;
96 if (isVisible())
97 return;
98 clear();
99
100 if (!activeChatsMode_) {
101 foreach(const UserResource& resource, contact_->userResourceList())
102 addResource(resource);
103 }
104 else {
105 foreach(QString resourceName, contact_->account()->hiddenChats(contact_->jid())) {
106 XMPP::Status::Type status;
107 const UserResourceList &rl = contact_->userResourceList();
108 UserResourceList::ConstIterator uit = rl.find(resourceName);
109 if (uit != rl.end() || (uit = rl.priority()) != rl.end())
110 status = makeSTATUS((*uit).status());
111 else
112 status = XMPP::Status::Offline;
113 addResource(status, resourceName);
114 }
115 }
116 }
117
activeChatsMode() const118 bool ResourceMenu::activeChatsMode() const
119 {
120 return activeChatsMode_;
121 }
122
setActiveChatsMode(bool activeChatsMode)123 void ResourceMenu::setActiveChatsMode(bool activeChatsMode)
124 {
125 activeChatsMode_ = activeChatsMode;
126 contactUpdated();
127 }
128