Update AUTHORS file
[centerim5.git] / src / Notify.cpp
blob17414dd676e8b4c19034db41412ee0f1d0d0ec8c
1 /*
2 * Copyright (C) 2010-2015 Petr Pavlu <setup@dagobah.cz>
4 * This file is part of CenterIM.
6 * CenterIM is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * CenterIM 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
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "Notify.h"
23 #include "Log.h"
25 #include <string.h> // memset
26 #include "gettext.h"
28 Notify *Notify::my_instance = NULL;
30 Notify *Notify::instance()
32 return my_instance;
35 Notify::UserInfoDialog::UserInfoDialog(const char *title) : SplitDialog(title)
37 setColorScheme("generalwindow");
39 treeview = new CppConsUI::TreeView(AUTOSIZE, AUTOSIZE);
40 setContainer(*treeview);
42 buttons->appendItem(
43 _("Done"), sigc::hide(sigc::mem_fun(this, &UserInfoDialog::close)));
45 onScreenResized();
48 void Notify::UserInfoDialog::onScreenResized()
50 moveResizeRect(CENTERIM->getScreenArea(CenterIM::CHAT_AREA));
53 void Notify::UserInfoDialog::update(
54 PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info)
56 treeview->clear();
57 CppConsUI::TreeView::NodeReference parent;
58 CppConsUI::Button *button;
60 // local information
61 PurpleAccount *account = purple_connection_get_account(gc);
62 PurpleBuddy *buddy = purple_find_buddy(account, who);
63 if (buddy) {
64 /* Note that we should always be able to find the specified buddy, unless
65 * something goes very wrong. */
66 button =
67 new CppConsUI::TreeView::ToggleCollapseButton(_("Local information"));
68 parent = treeview->appendNode(treeview->getRootNode(), *button);
70 button = new CppConsUI::Button(
71 CppConsUI::Button::FLAG_VALUE, _("Alias"), purple_buddy_get_alias(buddy));
72 treeview->appendNode(parent, *button);
74 time_t saved_time;
75 struct tm local_time;
76 const char *formatted_time;
78 // last_seen
79 if (PURPLE_BUDDY_IS_ONLINE(buddy))
80 formatted_time = _("Now");
81 else {
82 saved_time = static_cast<time_t>(
83 purple_blist_node_get_int(PURPLE_BLIST_NODE(buddy), "last_seen"));
84 if (saved_time && localtime_r(&saved_time, &local_time))
85 formatted_time = purple_date_format_long(&local_time);
86 else
87 formatted_time = _("Unknown");
89 button = new CppConsUI::Button(
90 CppConsUI::Button::FLAG_VALUE, _("Last seen"), formatted_time);
91 treeview->appendNode(parent, *button);
93 // last_activity
94 saved_time = static_cast<time_t>(
95 purple_blist_node_get_int(PURPLE_BLIST_NODE(buddy), "last_activity"));
96 if (saved_time && localtime_r(&saved_time, &local_time))
97 formatted_time = purple_date_format_long(&local_time);
98 else
99 formatted_time = _("Unknown");
100 button = new CppConsUI::Button(
101 CppConsUI::Button::FLAG_VALUE, _("Last activity"), formatted_time);
102 treeview->appendNode(parent, *button);
105 // remote information
106 button =
107 new CppConsUI::TreeView::ToggleCollapseButton(_("Remote information"));
108 parent = treeview->appendNode(treeview->getRootNode(), *button);
109 CppConsUI::TreeView::NodeReference subparent = parent;
110 for (GList *i = purple_notify_user_info_get_entries(user_info); i;
111 i = i->next) {
112 PurpleNotifyUserInfoEntry *entry =
113 reinterpret_cast<PurpleNotifyUserInfoEntry *>(i->data);
114 PurpleNotifyUserInfoEntryType type =
115 purple_notify_user_info_entry_get_type(entry);
117 const char *label = purple_notify_user_info_entry_get_label(entry);
118 if (!label)
119 continue;
120 const char *value = purple_notify_user_info_entry_get_value(entry);
121 char *nohtml = purple_markup_strip_html(value);
122 switch (type) {
123 case PURPLE_NOTIFY_USER_INFO_ENTRY_PAIR:
124 button = new CppConsUI::Button(
125 nohtml ? CppConsUI::Button::FLAG_VALUE : 0, label, nohtml);
126 treeview->appendNode(subparent, *button);
127 break;
128 case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_BREAK:
129 // ignore section breaks
130 break;
131 case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_HEADER:
132 button = new CppConsUI::TreeView::ToggleCollapseButton(label);
133 subparent = treeview->appendNode(parent, *button);
134 break;
135 default:
136 LOG->error(_("Unhandled userinfo entry type '%d'."), type);
137 break;
139 g_free(nohtml);
142 treeview->grabFocus();
145 Notify::Notify()
147 memset(&centerim_notify_ui_ops, 0, sizeof(centerim_notify_ui_ops));
149 // set the purple notify callbacks
150 centerim_notify_ui_ops.notify_message = notify_message_;
151 // centerim_notify_ui_ops.notify_email = notify_email_;
152 // centerim_notify_ui_ops.notify_emails = notify_emails_;
153 // centerim_notify_ui_ops.notify_formatted = notify_formatted_;
154 // centerim_notify_ui_ops.notify_searchresults = notify_searchresults_;
155 // centerim_notify_ui_ops.notify_searchresults_new_rows =
156 // notify_searchresults_new_rows_;
157 centerim_notify_ui_ops.notify_userinfo = notify_userinfo_;
158 // centerim_notify_ui_ops.notify_uri = notify_uri_;
159 centerim_notify_ui_ops.close_notify = close_notify_;
160 purple_notify_set_ui_ops(&centerim_notify_ui_ops);
163 Notify::~Notify()
165 purple_notify_set_ui_ops(NULL);
168 void Notify::init()
170 g_assert(!my_instance);
172 my_instance = new Notify;
175 void Notify::finalize()
177 g_assert(my_instance);
179 delete my_instance;
180 my_instance = NULL;
183 void Notify::onDialogClose(CppConsUI::Window &activator, PurpleNotifyType type)
185 CppConsUI::AbstractDialog *dialog =
186 dynamic_cast<CppConsUI::AbstractDialog *>(&activator);
187 g_assert(dialog);
189 if (notifications.find(dialog) != notifications.end()) {
190 notifications.erase(dialog);
191 purple_notify_close(type, dialog);
195 void Notify::onUserInfoDialogClose(CppConsUI::Window & /*activator*/, User user)
197 // the userinfo dialog is gone
198 userinfos.erase(user);
201 void *Notify::notify_message(PurpleNotifyMsgType /*type*/, const char *title,
202 const char *primary, const char *secondary)
204 char *text = g_strdup_printf("%s\n\n%s", primary, secondary);
205 CppConsUI::MessageDialog *dialog = new CppConsUI::MessageDialog(title, text);
206 g_free(text);
207 dialog->signal_close.connect(sigc::bind(
208 sigc::mem_fun(this, &Notify::onDialogClose), PURPLE_NOTIFY_MESSAGE));
209 dialog->show();
211 notifications.insert(dialog);
212 return dialog;
215 void *Notify::notify_userinfo(
216 PurpleConnection *gc, const char *who, PurpleNotifyUserInfo *user_info)
218 User user(purple_connection_get_account(gc), who);
219 UserInfo::iterator i = userinfos.find(user);
220 UserInfoDialog *dialog;
221 if (i == userinfos.end()) {
222 // create a new dialog to display this user info
223 char *title = g_strdup_printf(_("User information for %s"), who);
224 dialog = new UserInfoDialog(title);
225 g_free(title);
227 dialog->signal_close.connect(
228 sigc::bind(sigc::mem_fun(this, &Notify::onUserInfoDialogClose), user));
229 dialog->signal_close.connect(sigc::bind(
230 sigc::mem_fun(this, &Notify::onDialogClose), PURPLE_NOTIFY_USERINFO));
231 dialog->show();
233 notifications.insert(dialog);
234 userinfos[user] = dialog;
236 else {
237 // update already opened dialog
238 dialog = i->second;
241 dialog->update(gc, who, user_info);
242 return dialog;
245 void Notify::close_notify(PurpleNotifyType type, void *ui_handle)
247 // only some notifications are currently supported
248 g_assert(type == PURPLE_NOTIFY_MESSAGE || type == PURPLE_NOTIFY_USERINFO);
250 CppConsUI::AbstractDialog *dialog =
251 reinterpret_cast<CppConsUI::AbstractDialog *>(ui_handle);
252 if (notifications.find(dialog) != notifications.end()) {
253 notifications.erase(dialog);
254 // close the notification dialog if one is still opened
255 dialog->close();
259 /* vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab : */