Advertisement
Guest User

Untitled

a guest
Dec 13th, 2023
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.99 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2008-2012 Mike Massonnet <mmassonnet@xfce.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. */
  18.  
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22.  
  23. #ifdef HAVE_LOCALE_H
  24. #include <locale.h>
  25. #endif
  26.  
  27. #include <glib/gstdio.h>
  28. #include <gtk/gtk.h>
  29. #include <libxfce4ui/libxfce4ui.h>
  30.  
  31. #include "common.h"
  32. #include "plugin.h"
  33.  
  34. /*
  35. * Status Icon
  36. */
  37. G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  38.  
  39. static MyPlugin* status_icon_register (void);
  40. static void cb_status_icon_activate (MyPlugin *plugin);
  41. static void cb_status_icon_popup_menu (MyPlugin *plugin,
  42. guint button,
  43. guint activate_time);
  44. static void cb_status_icon_quit (MyPlugin *plugin);
  45. static void cb_status_icon_finalize (gpointer data,
  46. GObject *where_the_object_was);
  47. static void install_autostart_file (void);
  48. static void update_autostart_file (gboolean autostart);
  49.  
  50. /*
  51. * Plugin Registration
  52. */
  53.  
  54. gint
  55. main (gint argc,
  56. gchar *argv[])
  57. {
  58. MyPlugin *plugin;
  59.  
  60. gtk_init (&argc, &argv);
  61.  
  62. if (!WINDOWING_IS_X11 ())
  63. {
  64. g_message ("Systray icon is currently only supported on X11. Use the panel plugin instead.");
  65. return EXIT_FAILURE;
  66. }
  67.  
  68. plugin = status_icon_register ();
  69. if (plugin == NULL)
  70. return EXIT_FAILURE;
  71.  
  72. install_autostart_file ();
  73.  
  74. gtk_main ();
  75.  
  76. g_object_unref (plugin->status_icon);
  77.  
  78. return EXIT_SUCCESS;
  79. }
  80.  
  81. /*
  82. * Status Icon
  83. */
  84.  
  85. static gboolean
  86. cb_status_icon_is_embedded (gpointer user_data)
  87. {
  88. GtkStatusIcon *status_icon = user_data;
  89.  
  90. // Always return FALSE to avoid quitting the main loop
  91. return FALSE;
  92. }
  93.  
  94. static MyPlugin *
  95. status_icon_register (void)
  96. {
  97. MyPlugin *plugin = plugin_register ();
  98. if (plugin == NULL)
  99. return NULL;
  100.  
  101. /* Menu Position Func */
  102. plugin->menu_position_func = (GtkMenuPositionFunc)gtk_status_icon_position_menu;
  103.  
  104. /* Status Icon */
  105. if (gtk_icon_theme_has_icon (gtk_icon_theme_get_default (), "clipman"))
  106. {
  107. plugin->status_icon = gtk_status_icon_new_from_icon_name ("clipman");
  108. }
  109. else
  110. {
  111. plugin->status_icon = gtk_status_icon_new_from_icon_name ("edit-paste");
  112. }
  113. gtk_status_icon_set_tooltip_text (plugin->status_icon, _("Clipman"));
  114.  
  115. /* Signals */
  116. g_signal_connect_swapped (plugin->status_icon, "activate",
  117. G_CALLBACK (cb_status_icon_activate), plugin);
  118. plugin->popup_menu_id =
  119. g_signal_connect_swapped (plugin->status_icon, "popup-menu",
  120. G_CALLBACK (cb_status_icon_popup_menu), plugin);
  121. g_object_weak_ref (G_OBJECT (plugin->status_icon), cb_status_icon_finalize, plugin);
  122.  
  123. return plugin;
  124. }
  125.  
  126. static void
  127. cb_status_icon_activate (MyPlugin *plugin)
  128. {
  129. plugin_popup_menu (plugin);
  130. }
  131.  
  132. static void
  133. cb_status_icon_popup_menu (MyPlugin *plugin, guint button, guint activate_time)
  134. {
  135. GtkWidget *mi;
  136.  
  137. if (G_UNLIKELY (plugin->popup_menu == NULL))
  138. {
  139. plugin->popup_menu = gtk_menu_new ();
  140. mi = gtk_image_menu_item_new_from_stock (GTK_STOCK_PROPERTIES, NULL);
  141. gtk_menu_shell_append (GTK_MENU_SHELL (plugin->popup_menu), mi);
  142. g_signal_connect_swapped (mi, "activate", G_CALLBACK (plugin_configure), plugin);
  143. mi = gtk_check_menu_item_new_with_mnemonic (_("_Disable"));
  144. gtk_menu_shell_append (GTK_MENU_SHELL (plugin->popup_menu), mi);
  145. xfconf_g_property_bind (plugin->channel, "/tweaks/inhibit",
  146. G_TYPE_BOOLEAN, mi, "active");
  147. mi = gtk_image_menu_item_new_from_stock (GTK_STOCK_ABOUT, NULL);
  148. gtk_menu_shell_append (GTK_MENU_SHELL (plugin->popup_menu), mi);
  149. g_signal_connect_swapped (mi, "activate", G_CALLBACK (plugin_about), plugin);
  150. mi = gtk_separator_menu_item_new ();
  151. gtk_menu_shell_append (GTK_MENU_SHELL (plugin->popup_menu), mi);
  152. mi = gtk_image_menu_item_new_from_stock (GTK_STOCK_QUIT, NULL);
  153. g_signal_connect_swapped (mi, "activate", G_CALLBACK (cb_status_icon_quit), plugin);
  154. gtk_menu_shell_append (GTK_MENU_SHELL (plugin->popup_menu), mi);
  155. gtk_widget_show_all (plugin->popup_menu);
  156. }
  157.  
  158. gtk_menu_set_screen (GTK_MENU (plugin->popup_menu), gtk_status_icon_get_screen (plugin->status_icon));
  159.  
  160. if(!gtk_widget_has_grab(plugin->popup_menu))
  161. {
  162. gtk_grab_add(plugin->popup_menu);
  163. }
  164.  
  165. gtk_menu_popup (GTK_MENU (plugin->popup_menu), NULL, NULL,
  166. (GtkMenuPositionFunc)gtk_status_icon_position_menu, plugin->status_icon,
  167. 0, gtk_get_current_event_time ());
  168. }
  169.  
  170. static void
  171. cb_status_icon_quit (MyPlugin *plugin)
  172. {
  173. update_autostart_file (FALSE);
  174. gtk_status_icon_set_visible (plugin->status_icon, FALSE);
  175. gtk_main_quit ();
  176. }
  177.  
  178. static void
  179. cb_status_icon_finalize (gpointer data,
  180. GObject *where_the_object_was)
  181. {
  182. MyPlugin *plugin = data;
  183.  
  184. plugin_save (plugin);
  185. plugin_free (plugin);
  186. }
  187.  
  188. static void
  189. install_autostart_file (void)
  190. {
  191. gchar *sysfile;
  192. gchar *userfile;
  193. GKeyFile *keyfile;
  194. gchar *data;
  195.  
  196. sysfile = g_strdup (SYSCONFDIR"/xdg/autostart/"PACKAGE"-autostart.desktop");
  197. userfile = g_strdup_printf ("%s/autostart/"PACKAGE"-autostart.desktop", g_get_user_config_dir ());
  198.  
  199. if (!g_file_test (sysfile, G_FILE_TEST_EXISTS))
  200. {
  201. g_warning ("The autostart file (%s) is missing in the system installation", sysfile);
  202. goto out;
  203. }
  204.  
  205. /* Check if the user autostart file exists */
  206. if (g_file_test (userfile, G_FILE_TEST_EXISTS))
  207. {
  208. update_autostart_file (TRUE);
  209. goto out;
  210. }
  211.  
  212. /* Copy the file */
  213. keyfile = g_key_file_new ();
  214. g_key_file_load_from_file (keyfile, sysfile, G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
  215. g_key_file_set_boolean (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, FALSE);
  216. data = g_key_file_to_data (keyfile, NULL, NULL);
  217. g_file_set_contents (userfile, data, -1, NULL);
  218. g_free (data);
  219. g_key_file_free (keyfile);
  220.  
  221. out:
  222. g_free (sysfile);
  223. g_free (userfile);
  224. }
  225.  
  226. static void
  227. update_autostart_file (gboolean autostart)
  228. {
  229. gchar *userfile;
  230. GKeyFile *keyfile;
  231. gchar *data;
  232.  
  233. userfile = g_strdup_printf ("%s/autostart/"PACKAGE"-autostart.desktop", g_get_user_config_dir ());
  234.  
  235. /* Check if the user autostart file exists */
  236. if (!g_file_test (userfile, G_FILE_TEST_EXISTS))
  237. {
  238. g_warning ("The autostart file (%s) is not installed, what did you do?", userfile);
  239. g_free (userfile);
  240. return;
  241. }
  242.  
  243. /* Update the file */
  244. keyfile = g_key_file_new ();
  245. g_key_file_load_from_file (keyfile, userfile, G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
  246. g_key_file_set_boolean (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_HIDDEN, !autostart);
  247. g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_TRY_EXEC, "xfce4-clipman");
  248. g_key_file_set_string (keyfile, G_KEY_FILE_DESKTOP_GROUP, G_KEY_FILE_DESKTOP_KEY_EXEC, "xfce4-clipman");
  249. data = g_key_file_to_data (keyfile, NULL, NULL);
  250. g_file_set_contents (userfile, data, -1, NULL);
  251. g_free (data);
  252. g_key_file_free (keyfile);
  253. g_free (userfile);
  254. }
  255. G_GNUC_END_IGNORE_DEPRECATIONS
  256.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement