Debian Codegraph @

terminal/src/terminal-callbacks.c

raw
1 /**
2 * This file is a part of the Cairo-Dock project
3 *
4 * Copyright : (C) see the 'copyright' file.
5 * E-mail : see the 'copyright' file.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 3
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * 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/>.
18 */
19
20 #include <string.h>
21 #include <vte/vte.h>
22
23 #include "terminal-struct.h"
24 #include "terminal-callbacks.h"
25
26
27 static void _terminal_write_command_with_data (GtkWidget *pWidget, const gchar *cCommand, gchar *cData)
28 {
29 gchar *cCommandLine = g_strdup_printf ("%s \"%s\"", cCommand, cData);
30 vte_terminal_feed_child (VTE_TERMINAL (pWidget), cCommandLine, strlen (cCommandLine));
31 g_free (cCommandLine);
32 gldi_container_present (myContainer); // to bypass the focus steal prevention
33 gtk_widget_grab_focus (pWidget);
34 }
35 static void _terminal_paste (GtkMenuItem *menu_item, gpointer *data)
36 {
37 GtkWidget *pWidget = data[0];
38 gchar *cReceivedData = data[1];
39 cd_message ("%s (%s)", __func__, cReceivedData);
40 _terminal_write_command_with_data (pWidget, "", cReceivedData);
41 }
42
43 static void _terminal_cd (GtkMenuItem *menu_item, gpointer *data)
44 {
45 GtkWidget *pWidget = data[0];
46 gchar *cReceivedData = data[1];
47 cd_message ("%s (%s)", __func__, cReceivedData);
48 _terminal_write_command_with_data (pWidget, "cd", cReceivedData);
49 }
50 static void _terminal_cp (GtkMenuItem *menu_item, gpointer *data)
51 {
52 GtkWidget *pWidget = data[0];
53 gchar *cReceivedData = data[1];
54 cd_message ("%s (%s)", __func__, cReceivedData);
55 _terminal_write_command_with_data (pWidget, "cp -r", cReceivedData);
56 }
57 static void _terminal_mv (GtkMenuItem *menu_item, gpointer *data)
58 {
59 GtkWidget *pWidget = data[0];
60 gchar *cReceivedData = data[1];
61 cd_message ("%s (%s)", __func__, cReceivedData);
62 _terminal_write_command_with_data (pWidget, "mv", cReceivedData);
63 }
64 static void _terminal_rm (GtkMenuItem *menu_item, gpointer *data)
65 {
66 GtkWidget *pWidget = data[0];
67 gchar *cReceivedData = data[1];
68 cd_message ("%s (%s)", __func__, cReceivedData);
69 _terminal_write_command_with_data (pWidget, "rm -r", cReceivedData);
70 }
71
72
73 static GtkWidget *_terminal_build_menu (GtkWidget *pWidget, gchar *cReceivedData)
74 {
75 static gpointer *my_data = NULL;
76 if (my_data == NULL)
77 my_data = g_new0 (gpointer, 2);
78 my_data[0] = pWidget;
79 my_data[1] = cReceivedData;
80 GtkWidget *menu = gldi_menu_new (NULL);
81
82 gldi_menu_add_item (menu, D_("Paste"), GLDI_ICON_NAME_PASTE, G_CALLBACK(_terminal_paste), my_data);
83
84 gtk_menu_shell_append (GTK_MENU_SHELL (menu), gtk_separator_menu_item_new());
85
86 gldi_menu_add_item (menu, "cd", GLDI_ICON_NAME_JUMP_TO, G_CALLBACK(_terminal_cd), my_data);
87
88 gldi_menu_add_item (menu, "cp", GLDI_ICON_NAME_COPY, G_CALLBACK(_terminal_cp), my_data);
89
90 gldi_menu_add_item (menu, "mv", GLDI_ICON_NAME_GOTO_LAST, G_CALLBACK(_terminal_mv), my_data);
91
92 gldi_menu_add_item (menu, "rm", GLDI_ICON_NAME_DELETE, G_CALLBACK(_terminal_rm), my_data);
93
94 return menu;
95 }
96
97
98 void on_terminal_drag_data_received (GtkWidget *pWidget, GdkDragContext *dc, gint x, gint y, GtkSelectionData *selection_data, guint info, guint time, gpointer data)
99 {
100 static gchar *cReceivedData = NULL; // il faut pouvoir le passer aux callbacks. Le probleme c'est qu'il disparait a la fin de cette fonction, donc il faut le dupliquer. Comme on n'est pas sur que l'une des callbacks sera effectivement appelee, ce ne peut pas etre elles qui le desalloueront. Donc on le fait ici, d'ou la variable statique. On ne peut recevoir qu'un drop a la fois, donc pas de collision possible.
101 cd_message ("%s ()", __func__);
102
103 g_free (cReceivedData);
104 cReceivedData = NULL;
105 gchar *cText = (gchar *)gtk_selection_data_get_data (selection_data); // the data are actually 'const guchar*', but since we only allowed text and urls, it will be a string
106 g_return_if_fail (cText != NULL);
107
108 int length = strlen (cText);
109 if (cText[length-1] == '\n')
110 cText[--length] = '\0'; // on vire le retour chariot final.
111 if (cText[length-1] == '\r')
112 cText[--length] = '\0'; // on vire ce ... c'est quoi ce truc ??!
113 cd_message ("cReceivedData : %s", cText);
114
115 if (strncmp (cText, "file://", 7) == 0) // on gere le cas des URI.
116 {
117 GError *erreur = NULL;
118 cReceivedData = g_filename_from_uri (cText, NULL, &erreur);
119 if (erreur != NULL)
120 {
121 cd_message ("Terminal : %s", erreur->message);
122 g_error_free (erreur);
123 return ;
124 }
125 }
126 else
127 cReceivedData = g_strdup (cText);
128
129 GtkWidget *menu = _terminal_build_menu (pWidget, cReceivedData);
130
131 gtk_widget_show_all (menu);
132
133 gtk_menu_popup (GTK_MENU (menu),
134 NULL,
135 NULL,
136 NULL,
137 NULL,
138 1,
139 gtk_get_current_event_time ());
140
141 gtk_drag_finish (dc, TRUE, FALSE, time);
142 }