Sébastien Blin | 0575214 | 2017-10-03 11:25:02 -0400 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * Copyright (C) 2017 Savoir-faire Linux * |
| 3 | * Author: Nicolas Jäger <nicolas.jager@savoirfairelinux.com> * |
| 4 | * Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com> * |
| 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 General Public License * |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. * |
| 18 | ***************************************************************************/ |
| 19 | |
| 20 | #include "conversationpopupmenu.h" |
| 21 | |
| 22 | // GTK+ related |
| 23 | #include <glib/gi18n.h> |
| 24 | |
| 25 | // Lrc |
| 26 | #include <api/conversationmodel.h> |
| 27 | #include <api/contactmodel.h> |
| 28 | #include <api/contact.h> |
| 29 | |
| 30 | struct _ConversationPopupMenu |
| 31 | { |
| 32 | GtkMenu parent; |
| 33 | }; |
| 34 | |
| 35 | struct _ConversationPopupMenuClass |
| 36 | { |
| 37 | GtkMenuClass parent_class; |
| 38 | }; |
| 39 | |
| 40 | typedef struct _ConversationPopupMenuPrivate ConversationPopupMenuPrivate; |
| 41 | |
| 42 | struct _ConversationPopupMenuPrivate |
| 43 | { |
| 44 | GtkTreeView *treeview; |
| 45 | |
| 46 | AccountContainer* accountContainer_; |
| 47 | int row_; |
| 48 | }; |
| 49 | |
| 50 | G_DEFINE_TYPE_WITH_PRIVATE(ConversationPopupMenu, conversation_popup_menu, GTK_TYPE_MENU); |
| 51 | |
| 52 | #define CONVERSATION_POPUP_MENU_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), CONVERSATION_POPUP_MENU_TYPE, ConversationPopupMenuPrivate)) |
| 53 | |
| 54 | static void |
Sébastien Blin | d6787ad | 2017-11-27 09:40:38 -0500 | [diff] [blame] | 55 | copy_contact_info(G_GNUC_UNUSED GtkWidget *menu, ConversationPopupMenuPrivate* priv) |
| 56 | { |
| 57 | try |
| 58 | { |
| 59 | auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(priv->row_); |
| 60 | if (conversation.participants.empty()) return; |
| 61 | auto& contact = priv->accountContainer_->info.contactModel->getContact(conversation.participants.front()); |
| 62 | auto bestName = contact.registeredName.empty() ? contact.profileInfo.uri : contact.registeredName; |
| 63 | auto text = (gchar *)bestName.c_str(); |
| 64 | GtkClipboard* clip = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD); |
| 65 | gtk_clipboard_set_text(clip, text, -1); |
| 66 | clip = gtk_clipboard_get(GDK_SELECTION_PRIMARY); |
| 67 | gtk_clipboard_set_text(clip, text, -1); |
| 68 | } |
| 69 | catch (...) |
| 70 | { |
| 71 | g_warning("Can't get conversation at row %i", priv->row_); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 76 | remove_history_conversation(G_GNUC_UNUSED GtkWidget *menu, ConversationPopupMenuPrivate* priv) |
Sébastien Blin | 0575214 | 2017-10-03 11:25:02 -0400 | [diff] [blame] | 77 | { |
| 78 | try |
| 79 | { |
| 80 | auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(priv->row_); |
| 81 | priv->accountContainer_->info.conversationModel->clearHistory(conversation.uid); |
| 82 | } |
| 83 | catch (...) |
| 84 | { |
| 85 | g_warning("Can't get conversation at row %i", priv->row_); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | remove_conversation(G_GNUC_UNUSED GtkWidget *menu, ConversationPopupMenuPrivate* priv) |
| 91 | { |
| 92 | try |
| 93 | { |
| 94 | auto conversationUid = priv->accountContainer_->info.conversationModel->filteredConversation(priv->row_).uid; |
| 95 | priv->accountContainer_->info.conversationModel->removeConversation(conversationUid); |
| 96 | } |
| 97 | catch (...) |
| 98 | { |
| 99 | g_warning("Can't get conversation at row %i", priv->row_); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static void |
| 104 | block_conversation(G_GNUC_UNUSED GtkWidget *menu, ConversationPopupMenuPrivate* priv) |
| 105 | { |
| 106 | try |
| 107 | { |
| 108 | auto conversationUid = priv->accountContainer_->info.conversationModel->filteredConversation(priv->row_).uid; |
| 109 | priv->accountContainer_->info.conversationModel->removeConversation(conversationUid, true); |
| 110 | } |
| 111 | catch (...) |
| 112 | { |
| 113 | g_warning("Can't get conversation at row %i", priv->row_); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static void |
| 118 | add_conversation(G_GNUC_UNUSED GtkWidget *menu, ConversationPopupMenuPrivate* priv) |
| 119 | { |
| 120 | try |
| 121 | { |
| 122 | auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(priv->row_); |
| 123 | priv->accountContainer_->info.conversationModel->makePermanent(conversation.uid); |
| 124 | } |
| 125 | catch (...) |
| 126 | { |
| 127 | g_warning("Can't get conversation at row %i", priv->row_); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static void |
| 132 | place_call(G_GNUC_UNUSED GtkWidget *menu, ConversationPopupMenuPrivate* priv) |
| 133 | { |
| 134 | try |
| 135 | { |
| 136 | auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(priv->row_); |
| 137 | priv->accountContainer_->info.conversationModel->placeCall(conversation.uid); |
| 138 | } |
| 139 | catch (...) |
| 140 | { |
| 141 | g_warning("Can't get conversation at row %i", priv->row_); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Update the menu when the selected conversation in the treeview changes. |
| 147 | */ |
| 148 | static void |
| 149 | update(GtkTreeSelection *selection, ConversationPopupMenu *self) |
| 150 | { |
| 151 | ConversationPopupMenuPrivate *priv = CONVERSATION_POPUP_MENU_GET_PRIVATE(self); |
| 152 | /* clear the current menu */ |
| 153 | gtk_container_forall(GTK_CONTAINER(self), (GtkCallback)gtk_widget_destroy, nullptr); |
| 154 | |
| 155 | // Retrieve conversation |
| 156 | GtkTreeIter iter; |
| 157 | GtkTreeModel *model; |
| 158 | if (!gtk_tree_selection_get_selected(selection, &model, &iter)) return; |
| 159 | auto path = gtk_tree_model_get_path(model, &iter); |
| 160 | auto idx = gtk_tree_path_get_indices(path); |
| 161 | auto conversation = priv->accountContainer_->info.conversationModel->filteredConversation(idx[0]); |
| 162 | priv->row_ = idx[0]; |
| 163 | auto contactInfo = priv->accountContainer_->info.contactModel->getContact(conversation.participants.front()); |
| 164 | if (contactInfo.profileInfo.uri.empty()) return; |
| 165 | |
| 166 | // we always build a menu, however in some cases some or all of the conversations will be deactivated |
| 167 | // we prefer this to having an empty menu because GTK+ behaves weird in the empty menu case |
| 168 | auto place_call_conversation = gtk_menu_item_new_with_mnemonic(_("_Place call")); |
| 169 | gtk_menu_shell_append(GTK_MENU_SHELL(self), place_call_conversation); |
| 170 | g_signal_connect(place_call_conversation, "activate", G_CALLBACK(place_call), priv); |
Sébastien Blin | d6787ad | 2017-11-27 09:40:38 -0500 | [diff] [blame] | 171 | auto copy_name = gtk_menu_item_new_with_mnemonic(_("_Copy name")); |
| 172 | gtk_menu_shell_append(GTK_MENU_SHELL(self), copy_name); |
| 173 | g_signal_connect(copy_name, "activate", G_CALLBACK(copy_contact_info), priv); |
Sébastien Blin | 0575214 | 2017-10-03 11:25:02 -0400 | [diff] [blame] | 174 | if (contactInfo.profileInfo.type == lrc::api::profile::Type::TEMPORARY || |
| 175 | contactInfo.profileInfo.type == lrc::api::profile::Type::PENDING) { |
| 176 | // If we can add this conversation |
Sébastien Blin | 55bff9d | 2017-10-03 15:15:23 -0400 | [diff] [blame] | 177 | auto add_conversation_conversation = gtk_menu_item_new_with_mnemonic(_("_Add to conversations")); |
Sébastien Blin | 0575214 | 2017-10-03 11:25:02 -0400 | [diff] [blame] | 178 | gtk_menu_shell_append(GTK_MENU_SHELL(self), add_conversation_conversation); |
| 179 | g_signal_connect(add_conversation_conversation, "activate", G_CALLBACK(add_conversation), priv); |
| 180 | if (contactInfo.profileInfo.type == lrc::api::profile::Type::PENDING) { |
| 181 | auto rm_conversation_item = gtk_menu_item_new_with_mnemonic(_("_Discard invitation")); |
| 182 | gtk_menu_shell_append(GTK_MENU_SHELL(self), rm_conversation_item); |
| 183 | g_signal_connect(rm_conversation_item, "activate", G_CALLBACK(remove_conversation), priv); |
| 184 | auto block_conversation_item = gtk_menu_item_new_with_mnemonic(_("_Block invitations")); |
| 185 | gtk_menu_shell_append(GTK_MENU_SHELL(self), block_conversation_item); |
| 186 | g_signal_connect(block_conversation_item, "activate", G_CALLBACK(block_conversation), priv); |
| 187 | } |
| 188 | } else { |
Sébastien Blin | d6787ad | 2017-11-27 09:40:38 -0500 | [diff] [blame] | 189 | auto rm_history_conversation = gtk_menu_item_new_with_mnemonic(_("C_lear history")); |
Sébastien Blin | 0575214 | 2017-10-03 11:25:02 -0400 | [diff] [blame] | 190 | gtk_menu_shell_append(GTK_MENU_SHELL(self), rm_history_conversation); |
| 191 | g_signal_connect(rm_history_conversation, "activate", G_CALLBACK(remove_history_conversation), priv); |
| 192 | auto rm_conversation_item = gtk_menu_item_new_with_mnemonic(_("_Remove conversation")); |
| 193 | gtk_menu_shell_append(GTK_MENU_SHELL(self), rm_conversation_item); |
| 194 | g_signal_connect(rm_conversation_item, "activate", G_CALLBACK(remove_conversation), priv); |
| 195 | auto block_conversation_item = gtk_menu_item_new_with_mnemonic(_("_Block contact")); |
| 196 | gtk_menu_shell_append(GTK_MENU_SHELL(self), block_conversation_item); |
| 197 | g_signal_connect(block_conversation_item, "activate", G_CALLBACK(block_conversation), priv); |
| 198 | } |
| 199 | |
| 200 | /* show all conversations */ |
| 201 | gtk_widget_show_all(GTK_WIDGET(self)); |
| 202 | |
| 203 | } |
| 204 | |
| 205 | static void |
| 206 | conversation_popup_menu_dispose(GObject *object) |
| 207 | { |
| 208 | G_OBJECT_CLASS(conversation_popup_menu_parent_class)->dispose(object); |
| 209 | } |
| 210 | |
| 211 | static void |
| 212 | conversation_popup_menu_finalize(GObject *object) |
| 213 | { |
| 214 | G_OBJECT_CLASS(conversation_popup_menu_parent_class)->finalize(object); |
| 215 | } |
| 216 | |
| 217 | static void |
| 218 | conversation_popup_menu_class_init(ConversationPopupMenuClass *klass) |
| 219 | { |
| 220 | G_OBJECT_CLASS(klass)->finalize = conversation_popup_menu_finalize; |
| 221 | G_OBJECT_CLASS(klass)->dispose = conversation_popup_menu_dispose; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | static void |
| 226 | conversation_popup_menu_init(G_GNUC_UNUSED ConversationPopupMenu *self) |
| 227 | { |
| 228 | // nothing to do |
| 229 | } |
| 230 | |
| 231 | GtkWidget * |
| 232 | conversation_popup_menu_new (GtkTreeView *treeview, AccountContainer* accountContainer) |
| 233 | { |
| 234 | gpointer self = g_object_new(CONVERSATION_POPUP_MENU_TYPE, NULL); |
| 235 | ConversationPopupMenuPrivate *priv = CONVERSATION_POPUP_MENU_GET_PRIVATE(self); |
| 236 | priv->accountContainer_ = accountContainer; |
| 237 | |
| 238 | priv->treeview = treeview; |
| 239 | GtkTreeSelection *selection = gtk_tree_view_get_selection(priv->treeview); |
| 240 | |
| 241 | // build the menu for the first time |
| 242 | update(selection, CONVERSATION_POPUP_MENU(self)); |
| 243 | |
| 244 | return (GtkWidget *)self; |
| 245 | } |
| 246 | |
| 247 | gboolean |
| 248 | conversation_popup_menu_show(ConversationPopupMenu *self, GdkEventButton *event) |
| 249 | { |
| 250 | if (!self) return GDK_EVENT_PROPAGATE; |
| 251 | if (event->type == GDK_BUTTON_PRESS |
| 252 | && event->button == GDK_BUTTON_SECONDARY) { |
| 253 | // Show popup menu. Will be updated later. |
| 254 | gtk_menu_popup(GTK_MENU(self), NULL, NULL, NULL, NULL, event->button, event->time); |
| 255 | } |
| 256 | |
| 257 | return GDK_EVENT_PROPAGATE; // so that the conversation selection changes |
| 258 | } |