1 | /*
|
---|
2 | * psigrowlnotifier.cpp: Psi's interface to Growl
|
---|
3 | * Copyright (C) 2005 Remko Troncon
|
---|
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 | * You can also redistribute and/or modify this program under the
|
---|
11 | * terms of the Psi License, specified in the accompanied COPYING
|
---|
12 | * file, as published by the Psi Project; either dated January 1st,
|
---|
13 | * 2005, or (at your option) any later version.
|
---|
14 | *
|
---|
15 | * This program is distributed in the hope that it will be useful,
|
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | * GNU General Public License for more details.
|
---|
19 | *
|
---|
20 | * You should have received a copy of the GNU General Public License
|
---|
21 | * along with this library; if not, write to the Free Software
|
---|
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
23 | *
|
---|
24 | */
|
---|
25 |
|
---|
26 | #include <qstringlist.h>
|
---|
27 |
|
---|
28 | #include "common.h"
|
---|
29 | #include "psiaccount.h"
|
---|
30 | #include "avatars.h"
|
---|
31 | #include "growlnotifier.h"
|
---|
32 | #include "psigrowlnotifier.h"
|
---|
33 | #include "psievent.h"
|
---|
34 | #include "userlist.h"
|
---|
35 |
|
---|
36 | /*!
|
---|
37 | * A class representing the notification context, which will be passed to
|
---|
38 | * Growl, and then passed back when a notification is clicked.
|
---|
39 | */
|
---|
40 | class NotificationContext
|
---|
41 | {
|
---|
42 | public:
|
---|
43 | NotificationContext(PsiAccount* a, Jid j) : account_(a), jid_(j) { }
|
---|
44 | PsiAccount* account() { return account_; }
|
---|
45 | Jid jid() { return jid_; }
|
---|
46 |
|
---|
47 |
|
---|
48 | private:
|
---|
49 | PsiAccount* account_;
|
---|
50 | Jid jid_;
|
---|
51 | };
|
---|
52 |
|
---|
53 |
|
---|
54 | /*!
|
---|
55 | * (Private) constructor of the PsiGrowlNotifier.
|
---|
56 | * Initializes notifications and registers with Growl through GrowlNotifier.
|
---|
57 | */
|
---|
58 | PsiGrowlNotifier::PsiGrowlNotifier()
|
---|
59 | {
|
---|
60 | // Initialize all notifications
|
---|
61 | QStringList nots;
|
---|
62 | nots << QObject::tr("Contact becomes Available");
|
---|
63 | nots << QObject::tr("Contact becomes Unavailable");
|
---|
64 | nots << QObject::tr("Contact changes Status");
|
---|
65 | nots << QObject::tr("Incoming Message");
|
---|
66 | nots << QObject::tr("Incoming Headline");
|
---|
67 | nots << QObject::tr("Incoming File");
|
---|
68 |
|
---|
69 | // Initialize default notifications
|
---|
70 | QStringList defaults;
|
---|
71 | defaults << QObject::tr("Contact becomes Available");
|
---|
72 | defaults << QObject::tr("Incoming Message");
|
---|
73 | defaults << QObject::tr("Incoming Headline");
|
---|
74 | defaults << QObject::tr("Incoming File");
|
---|
75 |
|
---|
76 | // Register with Growl
|
---|
77 | gn_ = new GrowlNotifier(nots, defaults, "Psi");
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /*!
|
---|
82 | * Requests the global PsiGrowlNotifier instance.
|
---|
83 | * If PsiGrowlNotifier wasn't initialized yet, it is initialized.
|
---|
84 | *
|
---|
85 | * \see GrowlNotifier()
|
---|
86 | * \return A pointer to the PsiGrowlNotifier instance
|
---|
87 | */
|
---|
88 | PsiGrowlNotifier* PsiGrowlNotifier::instance()
|
---|
89 | {
|
---|
90 | if (!instance_)
|
---|
91 | instance_ = new PsiGrowlNotifier();
|
---|
92 |
|
---|
93 | return instance_;
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | /*!
|
---|
98 | * Requests a popup to be sent to Growl.
|
---|
99 | *
|
---|
100 | * \param account The requesting account.
|
---|
101 | * \param type The type of popup to be sent.
|
---|
102 | * \param jid The originating jid
|
---|
103 | * \param uli The originating userlist item. Can be NULL.
|
---|
104 | * \param event The originating event. Can be NULL.
|
---|
105 | */
|
---|
106 | void PsiGrowlNotifier::popup(PsiAccount* account, PsiPopup::PopupType type, const Jid& jid, const Resource& r, const UserListItem* uli, PsiEvent* event)
|
---|
107 | {
|
---|
108 | QString name;
|
---|
109 | QPixmap icon;
|
---|
110 | QString title, desc, contact;
|
---|
111 | QString statusTxt = status2txt(makeSTATUS(r.status()));
|
---|
112 | QString statusMsg = r.status().status();
|
---|
113 | if (uli && !uli->name().isEmpty())
|
---|
114 | contact = uli->name();
|
---|
115 | else
|
---|
116 | contact = jid.bare();
|
---|
117 |
|
---|
118 | #ifdef AVATARS
|
---|
119 | // FIXME: Lookup the client version
|
---|
120 | if (account->avatarFactory())
|
---|
121 | icon = account->avatarFactory()->getAvatar(jid, "Psi");
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | // Default value for the title
|
---|
125 | title = contact;
|
---|
126 |
|
---|
127 | switch(type) {
|
---|
128 | case PsiPopup::AlertOnline:
|
---|
129 | name = QObject::tr("Contact becomes Available");
|
---|
130 | title = QString("%1 (%2)").arg(contact).arg(statusTxt);
|
---|
131 | desc = statusMsg;
|
---|
132 | //icon = is->statusPQString(jid, r.status());
|
---|
133 | break;
|
---|
134 | case PsiPopup::AlertOffline:
|
---|
135 | name = QObject::tr("Contact becomes Unavailable");
|
---|
136 | title = QString("%1 (%2)").arg(contact).arg(statusTxt);
|
---|
137 | desc = statusMsg;
|
---|
138 | //icon = is->statusPQString(jid, r.status());
|
---|
139 | break;
|
---|
140 | case PsiPopup::AlertStatusChange:
|
---|
141 | name = QObject::tr("Contact changes Status");
|
---|
142 | title = QString("%1 (%2)").arg(contact).arg(statusTxt);
|
---|
143 | desc = statusMsg;
|
---|
144 | //icon = is->statusPQString(jid, r.status());
|
---|
145 | break;
|
---|
146 | case PsiPopup::AlertMessage: {
|
---|
147 | name = QObject::tr("Incoming Message");
|
---|
148 | title = QObject::tr("%1 says:").arg(contact);
|
---|
149 | const Message* jmessage = &((MessageEvent *)event)->message();
|
---|
150 | desc = jmessage->body();
|
---|
151 | //icon = IconsetFactory::iconPQString("psi/message");
|
---|
152 | break;
|
---|
153 | }
|
---|
154 | case PsiPopup::AlertChat: {
|
---|
155 | name = QObject::tr("Incoming Message");
|
---|
156 | const Message* jmessage = &((MessageEvent *)event)->message();
|
---|
157 | desc = jmessage->body();
|
---|
158 | //icon = IconsetFactory::iconPQString("psi/start-chat");
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | case PsiPopup::AlertHeadline: {
|
---|
162 | name = QObject::tr("Incoming Headline");
|
---|
163 | const Message* jmessage = &((MessageEvent *)event)->message();
|
---|
164 | if ( !jmessage->subject().isEmpty())
|
---|
165 | title = jmessage->subject();
|
---|
166 | desc = jmessage->body();
|
---|
167 | //icon = IconsetFactory::iconPQString("psi/headline");
|
---|
168 | break;
|
---|
169 | }
|
---|
170 | case PsiPopup::AlertFile:
|
---|
171 | name = QObject::tr("Incoming File");
|
---|
172 | desc = QObject::tr("[Incoming File]");
|
---|
173 | //icon = IconsetFactory::iconPQString("psi/file");
|
---|
174 | break;
|
---|
175 | default:
|
---|
176 | break;
|
---|
177 | }
|
---|
178 |
|
---|
179 | // Notify Growl
|
---|
180 | NotificationContext* context = new NotificationContext(account, jid);
|
---|
181 | gn_->notify(name, title, desc, icon, false, this, SLOT(notificationClicked(void*)), SLOT(notificationTimedOut(void*)), context);
|
---|
182 | }
|
---|
183 |
|
---|
184 | void PsiGrowlNotifier::notificationClicked(void* c)
|
---|
185 | {
|
---|
186 | NotificationContext* context = (NotificationContext*) c;
|
---|
187 | context->account()->actionDefault(context->jid());
|
---|
188 | delete context;
|
---|
189 | }
|
---|
190 |
|
---|
191 | void PsiGrowlNotifier::notificationTimedOut(void* c)
|
---|
192 | {
|
---|
193 | NotificationContext* context = (NotificationContext*) c;
|
---|
194 | delete context;
|
---|
195 | }
|
---|
196 |
|
---|
197 | PsiGrowlNotifier* PsiGrowlNotifier::instance_ = 0;
|
---|