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/>.
25 #include <string.h> // memset
28 Notify
*Notify::my_instance
= NULL
;
30 Notify
*Notify::instance()
35 Notify::UserInfoDialog::UserInfoDialog(const char *title
) : SplitDialog(title
)
37 setColorScheme("generalwindow");
39 treeview
= new CppConsUI::TreeView(AUTOSIZE
, AUTOSIZE
);
40 setContainer(*treeview
);
43 _("Done"), sigc::hide(sigc::mem_fun(this, &UserInfoDialog::close
)));
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
)
57 CppConsUI::TreeView::NodeReference parent
;
58 CppConsUI::Button
*button
;
61 PurpleAccount
*account
= purple_connection_get_account(gc
);
62 PurpleBuddy
*buddy
= purple_find_buddy(account
, who
);
64 /* Note that we should always be able to find the specified buddy, unless
65 * something goes very wrong. */
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
);
76 const char *formatted_time
;
79 if (PURPLE_BUDDY_IS_ONLINE(buddy
))
80 formatted_time
= _("Now");
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
);
87 formatted_time
= _("Unknown");
89 button
= new CppConsUI::Button(
90 CppConsUI::Button::FLAG_VALUE
, _("Last seen"), formatted_time
);
91 treeview
->appendNode(parent
, *button
);
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
);
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
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
;
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
);
120 const char *value
= purple_notify_user_info_entry_get_value(entry
);
121 char *nohtml
= purple_markup_strip_html(value
);
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
);
128 case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_BREAK
:
129 // ignore section breaks
131 case PURPLE_NOTIFY_USER_INFO_ENTRY_SECTION_HEADER
:
132 button
= new CppConsUI::TreeView::ToggleCollapseButton(label
);
133 subparent
= treeview
->appendNode(parent
, *button
);
136 LOG
->error(_("Unhandled userinfo entry type '%d'."), type
);
142 treeview
->grabFocus();
147 memset(¢erim_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(¢erim_notify_ui_ops
);
165 purple_notify_set_ui_ops(NULL
);
170 g_assert(!my_instance
);
172 my_instance
= new Notify
;
175 void Notify::finalize()
177 g_assert(my_instance
);
183 void Notify::onDialogClose(CppConsUI::Window
&activator
, PurpleNotifyType type
)
185 CppConsUI::AbstractDialog
*dialog
=
186 dynamic_cast<CppConsUI::AbstractDialog
*>(&activator
);
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
);
207 dialog
->signal_close
.connect(sigc::bind(
208 sigc::mem_fun(this, &Notify::onDialogClose
), PURPLE_NOTIFY_MESSAGE
));
211 notifications
.insert(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
);
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
));
233 notifications
.insert(dialog
);
234 userinfos
[user
] = dialog
;
237 // update already opened dialog
241 dialog
->update(gc
, who
, user_info
);
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
259 /* vim: set tabstop=2 shiftwidth=2 textwidth=80 expandtab : */