Updated Spanish translation
[anjuta-git-plugin.git] / plugins / message-view / plugin.c
blob26d10836b076342f2e1b9c239a9ab21df9d47d0b
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 plugin.c
4 Copyright (C) 2004 Johannes Schmid
6 This program 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 This program 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, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <config.h>
22 #include <libanjuta/anjuta-shell.h>
23 #include <libanjuta/anjuta-debug.h>
24 #include <libanjuta/interfaces/ianjuta-message-manager.h>
25 #include <libanjuta/interfaces/ianjuta-preferences.h>
27 #include "plugin.h"
28 #include "anjuta-msgman.h"
30 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-message-manager.ui"
31 #define PREFS_GLADE PACKAGE_DATA_DIR"/glade/anjuta-message-manager-plugin.glade"
32 #define ICON_FILE "preferences-messages.png"
34 /* Pixmaps */
35 #define ANJUTA_PIXMAP_MESSAGES "anjuta-message-view.png"
36 #define ANJUTA_PIXMAP_PREV_MESSAGE "anjuta-go-message-prev"
37 #define ANJUTA_PIXMAP_NEXT_MESSAGE "anjuta-go-message-next"
39 /* Stock icons */
40 #define ANJUTA_STOCK_MESSAGES "anjuta-messages"
41 #define ANJUTA_STOCK_PREV_MESSAGE "anjuta-prev-message"
42 #define ANJUTA_STOCK_NEXT_MESSAGE "anjuta-next-message"
44 static void on_next_message(GtkAction* menuitem, MessageViewPlugin *plugin)
46 AnjutaMsgman* msgman = ANJUTA_MSGMAN(plugin->msgman);
47 MessageView* view = anjuta_msgman_get_current_view(msgman);
48 if (view != NULL)
49 message_view_next(view);
52 static void on_prev_message(GtkAction* menuitem, MessageViewPlugin *plugin)
54 AnjutaMsgman* msgman = ANJUTA_MSGMAN(plugin->msgman);
55 MessageView* view = anjuta_msgman_get_current_view(msgman);
56 if (view != NULL)
57 message_view_previous(view);
60 static void on_save_message(GtkAction* menuitem, MessageViewPlugin *plugin)
62 AnjutaMsgman* msgman = ANJUTA_MSGMAN(plugin->msgman);
63 MessageView* view = anjuta_msgman_get_current_view(msgman);
64 if (view != NULL)
65 message_view_save(view);
68 static GtkActionEntry actions_goto[] = {
69 { "ActionMenuGoto", NULL, N_("_Goto"), NULL, NULL, NULL},
70 { "ActionMessageNext", ANJUTA_STOCK_NEXT_MESSAGE,
71 N_("_Next Message"), "<control><alt>n",
72 N_("Next message"),
73 G_CALLBACK (on_next_message)},
74 { "ActionMessagePrev", ANJUTA_STOCK_PREV_MESSAGE,
75 N_("_Previous Message"), "<control><alt>p",
76 N_("Previous message"),
77 G_CALLBACK (on_prev_message)},
78 { "ActionMessageSave", NULL,
79 N_("_Save Message"), NULL,
80 N_("Save message"),
81 G_CALLBACK (on_save_message)}
84 static void on_view_changed(AnjutaMsgman* msgman, MessageViewPlugin* plugin)
86 AnjutaUI* ui = anjuta_shell_get_ui (ANJUTA_PLUGIN(plugin)->shell, NULL);
87 GtkAction* action_next = anjuta_ui_get_action (ui, "ActionGroupGotoMessages",
88 "ActionMessageNext");
89 GtkAction* action_prev = anjuta_ui_get_action (ui, "ActionGroupGotoMessages",
90 "ActionMessagePrev");
91 gboolean sensitive = (anjuta_msgman_get_current_view(msgman) != NULL);
92 if (sensitive)
93 anjuta_shell_present_widget (ANJUTA_PLUGIN (plugin)->shell,
94 GTK_WIDGET(msgman), NULL);
95 g_object_set (G_OBJECT (action_next), "sensitive", sensitive, NULL);
96 g_object_set (G_OBJECT (action_prev), "sensitive", sensitive, NULL);
99 static gpointer parent_class;
101 static void
102 register_stock_icons (AnjutaPlugin *plugin)
104 static gboolean registered = FALSE;
106 if (registered)
107 return;
108 registered = TRUE;
110 /* Register stock icons */
111 BEGIN_REGISTER_ICON (plugin);
112 REGISTER_ICON (ICON_FILE, "message-manager-plugin-icon");
113 REGISTER_ICON (ANJUTA_PIXMAP_MESSAGES, ANJUTA_STOCK_MESSAGES);
114 REGISTER_ICON_FULL (ANJUTA_PIXMAP_NEXT_MESSAGE, ANJUTA_STOCK_NEXT_MESSAGE);
115 REGISTER_ICON_FULL (ANJUTA_PIXMAP_PREV_MESSAGE, ANJUTA_STOCK_PREV_MESSAGE);
116 END_REGISTER_ICON;
119 #if 0 /* Disable session saving/loading until a way is found to avoid
120 * number of message panes infinitely growing */
121 static void
122 on_session_save (AnjutaShell *shell, AnjutaSessionPhase phase,
123 AnjutaSession *session, MessageViewPlugin *plugin)
125 gboolean success;
126 const gchar *dir;
127 gchar *messages_file;
128 AnjutaSerializer *serializer;
130 if (phase != ANJUTA_SESSION_PHASE_NORMAL)
131 return;
133 dir = anjuta_session_get_session_directory (session);
134 messages_file = g_build_filename (dir, "messages.txt", NULL);
135 serializer = anjuta_serializer_new (messages_file,
136 ANJUTA_SERIALIZER_WRITE);
137 if (!serializer)
139 g_free (messages_file);
140 return;
142 success = anjuta_msgman_serialize (ANJUTA_MSGMAN (plugin->msgman),
143 serializer);
144 g_object_unref (serializer);
145 if (!success)
147 g_warning ("Serialization failed: deleting %s", messages_file);
148 unlink (messages_file);
150 g_free (messages_file);
153 static void
154 on_session_load (AnjutaShell *shell, AnjutaSessionPhase phase,
155 AnjutaSession *session, MessageViewPlugin *plugin)
157 const gchar *dir;
158 gchar *messages_file;
159 AnjutaSerializer *serializer;
161 if (phase != ANJUTA_SESSION_PHASE_NORMAL)
162 return;
164 dir = anjuta_session_get_session_directory (session);
165 messages_file = g_build_filename (dir, "messages.txt", NULL);
166 serializer = anjuta_serializer_new (messages_file,
167 ANJUTA_SERIALIZER_READ);
168 if (!serializer)
170 g_free (messages_file);
171 return;
173 anjuta_msgman_remove_all_views (ANJUTA_MSGMAN (plugin->msgman));
174 anjuta_msgman_deserialize (ANJUTA_MSGMAN (plugin->msgman), serializer);
175 g_object_unref (serializer);
176 g_free (messages_file);
178 #endif
180 static gboolean
181 activate_plugin (AnjutaPlugin *plugin)
183 AnjutaUI *ui;
184 GtkWidget *popup;
185 MessageViewPlugin *mv_plugin;
186 static gboolean initialized = FALSE;
188 DEBUG_PRINT ("MessageViewPlugin: Activating MessageView plugin ...");
189 mv_plugin = ANJUTA_PLUGIN_MESSAGE_VIEW (plugin);
191 if (!initialized)
193 register_stock_icons (plugin);
195 ui = anjuta_shell_get_ui (plugin->shell, NULL);
196 mv_plugin->action_group =
197 anjuta_ui_add_action_group_entries (ui, "ActionGroupGotoMessages",
198 _("Next/Prev Message"),
199 actions_goto,
200 G_N_ELEMENTS (actions_goto),
201 GETTEXT_PACKAGE, TRUE, plugin);
202 mv_plugin->uiid = anjuta_ui_merge (ui, UI_FILE);
203 popup = gtk_ui_manager_get_widget (GTK_UI_MANAGER (ui), "/PopupMessageView");
204 mv_plugin->msgman =
205 anjuta_msgman_new(anjuta_shell_get_preferences(plugin->shell, NULL), popup);
206 g_signal_connect(G_OBJECT(mv_plugin->msgman), "view_changed",
207 G_CALLBACK(on_view_changed), mv_plugin);
208 GtkAction* action_next = anjuta_ui_get_action (ui, "ActionGroupGotoMessages",
209 "ActionMessageNext");
210 GtkAction* action_prev = anjuta_ui_get_action (ui, "ActionGroupGotoMessages",
211 "ActionMessagePrev");
212 g_object_set (G_OBJECT (action_next), "sensitive", FALSE, NULL);
213 g_object_set (G_OBJECT (action_prev), "sensitive", FALSE, NULL);
215 anjuta_shell_add_widget (plugin->shell, mv_plugin->msgman,
216 "AnjutaMessageView", _("Messages"),
217 "message-manager-plugin-icon",
218 ANJUTA_SHELL_PLACEMENT_BOTTOM, NULL);
219 #if 0
220 /* Connect to save and load session */
221 g_signal_connect (G_OBJECT (plugin->shell), "save-session",
222 G_CALLBACK (on_session_save), plugin);
223 g_signal_connect (G_OBJECT (plugin->shell), "load-session",
224 G_CALLBACK (on_session_load), plugin);
225 #endif
226 initialized = TRUE;
227 return TRUE;
230 static gboolean
231 deactivate_plugin (AnjutaPlugin *plugin)
233 MessageViewPlugin *mplugin;
234 AnjutaUI *ui = anjuta_shell_get_ui (plugin->shell, NULL);
236 DEBUG_PRINT ("MessageViewPlugin: Dectivating message view plugin ...");
238 mplugin = ANJUTA_PLUGIN_MESSAGE_VIEW (plugin);
239 #if 0
240 /* Disconnect signals */
241 g_signal_handlers_disconnect_by_func (G_OBJECT (plugin->shell),
242 G_CALLBACK (on_session_save),
243 plugin);
244 g_signal_handlers_disconnect_by_func (G_OBJECT (plugin->shell),
245 G_CALLBACK (on_session_load),
246 plugin);
247 #endif
248 /* Widget is destroyed as soon as it is removed */
249 anjuta_shell_remove_widget (plugin->shell, mplugin->msgman, NULL);
250 anjuta_ui_unmerge (ui, mplugin->uiid);
251 anjuta_ui_remove_action_group (ui, mplugin->action_group);
253 mplugin->action_group = NULL;
254 mplugin->msgman = NULL;
255 mplugin->uiid = 0;
257 return TRUE;
260 static void
261 message_view_plugin_dispose (GObject *obj)
263 // MessageViewPlugin *plugin = ANJUTA_PLUGIN_MESSAGE_VIEW (obj);
264 GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (obj));
267 static void
268 message_view_plugin_finalize (GObject *obj)
270 // MessageViewPlugin *plugin = ANJUTA_PLUGIN_MESSAGE_VIEW (obj);
271 GNOME_CALL_PARENT (G_OBJECT_CLASS, finalize, (obj));
274 static void
275 message_view_plugin_instance_init (GObject *obj)
277 MessageViewPlugin *plugin = ANJUTA_PLUGIN_MESSAGE_VIEW (obj);
278 plugin->action_group = NULL;
279 plugin->msgman = NULL;
280 plugin->uiid = 0;
283 static void
284 message_view_plugin_class_init (GObjectClass *klass)
286 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
288 parent_class = g_type_class_peek_parent (klass);
290 plugin_class->activate = activate_plugin;
291 plugin_class->deactivate = deactivate_plugin;
292 klass->dispose = message_view_plugin_dispose;
293 klass->finalize = message_view_plugin_finalize;
297 * IAnjutaMessagerManager interface implementation
299 static IAnjutaMessageView*
300 ianjuta_msgman_add_view (IAnjutaMessageManager *plugin,
301 const gchar *file, const gchar *icon,
302 GError **e)
304 MessageView* message_view;
305 AnjutaShell* shell = ANJUTA_PLUGIN(plugin)->shell;
306 GtkWidget *msgman = ANJUTA_PLUGIN_MESSAGE_VIEW (plugin)->msgman;
307 anjuta_shell_present_widget(shell, msgman, NULL);
308 message_view = anjuta_msgman_add_view (ANJUTA_MSGMAN (msgman), file, icon);
309 return IANJUTA_MESSAGE_VIEW (message_view);
312 static void
313 ianjuta_msgman_remove_view (IAnjutaMessageManager *plugin,
314 IAnjutaMessageView * view,
315 GError **e)
317 GtkWidget *msgman = ANJUTA_PLUGIN_MESSAGE_VIEW (plugin)->msgman;
318 anjuta_msgman_remove_view (ANJUTA_MSGMAN (msgman), MESSAGE_VIEW (view));
321 static IAnjutaMessageView*
322 ianjuta_msgman_get_current_view (IAnjutaMessageManager *plugin,
323 GError **e)
325 GtkWidget *msgman = ANJUTA_PLUGIN_MESSAGE_VIEW (plugin)->msgman;
326 return IANJUTA_MESSAGE_VIEW (anjuta_msgman_get_current_view
327 (ANJUTA_MSGMAN (msgman)));
330 static IAnjutaMessageView*
331 ianjuta_msgman_get_view_by_name (IAnjutaMessageManager *plugin,
332 const gchar * name,
333 GError ** e)
335 GtkWidget *msgman = ANJUTA_PLUGIN_MESSAGE_VIEW (plugin)->msgman;
336 return IANJUTA_MESSAGE_VIEW (anjuta_msgman_get_view_by_name
337 (ANJUTA_MSGMAN (msgman), name));
340 static GList *
341 ianjuta_msgman_get_all_views (IAnjutaMessageManager *plugin,
342 GError ** e)
344 GtkWidget *msgman = ANJUTA_PLUGIN_MESSAGE_VIEW (plugin)->msgman;
345 return anjuta_msgman_get_all_views (ANJUTA_MSGMAN (msgman));
348 static void
349 ianjuta_msgman_set_current_view (IAnjutaMessageManager *plugin,
350 IAnjutaMessageView *message_view,
351 GError ** e)
353 AnjutaShell* shell;
354 GtkWidget *msgman = ANJUTA_PLUGIN_MESSAGE_VIEW (plugin)->msgman;
355 anjuta_msgman_set_current_view (ANJUTA_MSGMAN (msgman),
356 MESSAGE_VIEW (message_view));
358 /* Ensure the message-view is visible! */
359 g_object_get(G_OBJECT(plugin), "shell", &shell, NULL);
362 static void
363 ianjuta_msgman_set_view_title (IAnjutaMessageManager *plugin,
364 IAnjutaMessageView *message_view,
365 const gchar *title, GError ** e)
367 GtkWidget *msgman = ANJUTA_PLUGIN_MESSAGE_VIEW (plugin)->msgman;
368 anjuta_msgman_set_view_title (ANJUTA_MSGMAN (msgman),
369 MESSAGE_VIEW (message_view), title);
372 static void
373 ianjuta_msgman_iface_init (IAnjutaMessageManagerIface *iface)
375 iface->add_view = ianjuta_msgman_add_view;
376 iface->remove_view = ianjuta_msgman_remove_view;
377 iface->get_view_by_name = ianjuta_msgman_get_view_by_name;
378 iface->get_current_view = ianjuta_msgman_get_current_view;
379 iface->set_current_view = ianjuta_msgman_set_current_view;
380 iface->get_all_views = ianjuta_msgman_get_all_views;
381 iface->set_view_title = ianjuta_msgman_set_view_title;
384 static guint notify_id;
386 static void
387 ipreferences_merge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
389 GladeXML *gxml;
390 MessageViewPlugin* plugin = ANJUTA_PLUGIN_MESSAGE_VIEW (ipref);
391 /* Create the messages preferences page */
392 gxml = glade_xml_new (PREFS_GLADE, "preferences_dialog_messages", NULL);
393 anjuta_preferences_add_page (prefs, gxml,
394 "Messages", _("Messages"), ICON_FILE);
395 notify_id = anjuta_preferences_notify_add (prefs, MESSAGES_TABS_POS,
396 on_gconf_notify_message_pref, plugin->msgman, NULL);
398 g_object_unref (gxml);
401 static void
402 ipreferences_unmerge(IAnjutaPreferences* ipref, AnjutaPreferences* prefs, GError** e)
404 anjuta_preferences_notify_remove(prefs, notify_id);
405 anjuta_preferences_remove_page(prefs, _("Messages"));
408 static void
409 ipreferences_iface_init(IAnjutaPreferencesIface* iface)
411 iface->merge = ipreferences_merge;
412 iface->unmerge = ipreferences_unmerge;
415 ANJUTA_PLUGIN_BEGIN (MessageViewPlugin, message_view_plugin);
416 ANJUTA_PLUGIN_ADD_INTERFACE(ianjuta_msgman, IANJUTA_TYPE_MESSAGE_MANAGER);
417 ANJUTA_PLUGIN_ADD_INTERFACE(ipreferences, IANJUTA_TYPE_PREFERENCES);
418 ANJUTA_PLUGIN_END;
420 ANJUTA_SIMPLE_PLUGIN (MessageViewPlugin, message_view_plugin);