planner-xml-planner-plugin.c 5.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2003-2004 Imendio HB
 * Copyright (C) 2004 Lincoln Phipps <lincoln.phipps@openmutual.net>
 * Copyright (C) 2003 CodeFactory AB
 * Copyright (C) 2003 Richard Hult <richard@imendio.com>
 * Copyright (C) 2003 Mikael Hallendal <micke@imendio.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <string.h>
27
#include <glib.h>
28 29 30
#include <glade/glade.h>
#include <gtk/gtk.h>
#include <libgnome/gnome-i18n.h>
31
#include "planner-conf.h"
32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "planner-window.h"
#include "planner-plugin.h"

#define GCONF_PATH "/apps/planner"

struct _PlannerPluginPriv {
	PlannerWindow *main_window;
	GtkWidget     *dialog;
	GtkWidget     *local_rbutton;
	GtkWidget     *local_fileentry;
	GtkWidget     *server_rbutton;
	GtkWidget     *server_entry;
};

46 47
static void xml_planner_plugin_export (GtkAction         *action,
				       gpointer           user_data);
48 49 50 51
void        plugin_init               (PlannerPlugin     *plugin,
				       PlannerWindow     *main_window);
void        plugin_exit               (PlannerPlugin     *plugin);

52 53 54

static GtkActionEntry action_entries[] = {
	{ "XML Planner Export", NULL,
Richard Hult's avatar
Richard Hult committed
55 56
	  N_("Planner 0.11 Format"), NULL,
	  N_("Export project to a file suitable for Planner 0.11"),
57
	  G_CALLBACK (xml_planner_plugin_export) },
58
};
59 60 61
static guint n_action_entries = G_N_ELEMENTS (action_entries);


62 63 64 65 66 67

static gchar *
get_last_dir (void)
{
	gchar       *last_dir;
	
68
	last_dir = planner_conf_get_string ("/general/last_dir", NULL);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	
	if (last_dir == NULL) {
		last_dir = g_strdup (g_get_home_dir ());
	}
	
	if (last_dir[strlen (last_dir)] != G_DIR_SEPARATOR) {
		gchar *tmp;
		
		tmp = g_strconcat (last_dir, G_DIR_SEPARATOR_S, NULL);
		g_free (last_dir);
		last_dir = tmp;
	}

	return last_dir;
}

static void
86 87
xml_planner_plugin_export (GtkAction         *action,
			   gpointer           user_data)
88 89 90 91 92 93 94 95
{
	PlannerPluginPriv *priv = PLANNER_PLUGIN (user_data)->priv;
	MrpProject        *project;
	GError            *error = NULL;
	GtkWidget         *file_sel;
	GtkWidget         *dialog;
	gint               response;
	const gchar       *filename = NULL;
Richard Hult's avatar
Richard Hult committed
96
	gchar             *real_filename;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
	gchar             *last_dir;

	file_sel = gtk_file_selection_new (_("Export"));

	last_dir = get_last_dir ();
	gtk_file_selection_set_filename (GTK_FILE_SELECTION (file_sel), last_dir);
	g_free (last_dir);

	gtk_window_set_modal (GTK_WINDOW (file_sel), TRUE);

	while (TRUE) {
		response = gtk_dialog_run (GTK_DIALOG (file_sel));
		if (response != GTK_RESPONSE_OK) {
			gtk_widget_destroy (file_sel);
			return;
		}
		
		filename = gtk_file_selection_get_filename (
			GTK_FILE_SELECTION (file_sel));

Richard Hult's avatar
Richard Hult committed
117 118 119 120 121 122 123 124

		if (!strstr (filename, ".mrproject") || !strstr (filename, ".planner")) {
			real_filename = g_strconcat (filename, ".mrproject", NULL);
		} else {
			real_filename = g_strdup (filename);
		}
	
		if (g_file_test (real_filename, G_FILE_TEST_EXISTS)) {
125 126 127 128 129 130 131
			dialog = gtk_message_dialog_new (GTK_WINDOW (priv->main_window),
							 GTK_DIALOG_MODAL |
							 GTK_DIALOG_DESTROY_WITH_PARENT,
							 GTK_MESSAGE_WARNING,
							 GTK_BUTTONS_YES_NO,
							 _("File \"%s\" exists, "
							   "do you want to overwrite it?"),
Richard Hult's avatar
Richard Hult committed
132
							 real_filename);
133 134 135 136 137 138 139 140
			
			response = gtk_dialog_run (GTK_DIALOG (dialog));
			gtk_widget_destroy (dialog);
			
			switch (response) {
			case GTK_RESPONSE_YES:
				break;
			default:
Richard Hult's avatar
Richard Hult committed
141
				g_free (real_filename);
142 143 144 145 146 147 148 149 150
				continue;
			}
		}

		gtk_widget_hide (file_sel);
		break;
	} 

	project = planner_window_get_project (priv->main_window);
Richard Hult's avatar
Richard Hult committed
151 152

	if (!mrp_project_export (project, real_filename,
153 154 155 156 157 158
				 "Planner XML pre-0.12",
				 TRUE,
				 &error)) {
		g_warning ("Error while export to Planner XML: %s", error->message);
	}

Richard Hult's avatar
Richard Hult committed
159
	last_dir = g_path_get_dirname (real_filename);
160
	planner_conf_set_string ("/general/last_dir", last_dir, NULL);
161
	g_free (last_dir);
162

Richard Hult's avatar
Richard Hult committed
163
	g_free (real_filename);
164 165 166 167 168 169 170 171

	gtk_widget_destroy (file_sel);
}

G_MODULE_EXPORT void 
plugin_init (PlannerPlugin *plugin, PlannerWindow *main_window)
{
	PlannerPluginPriv *priv;
172 173 174
	GtkUIManager      *ui;
	GtkActionGroup    *actions;
	GError            *error = NULL;
175 176 177 178
	
	priv = g_new0 (PlannerPluginPriv, 1);
	plugin->priv = priv;
	priv->main_window = main_window;
179 180 181

	/* Create the actions, get the ui manager and merge the whole */
	actions = gtk_action_group_new ("XML plugin actions");
182 183
	gtk_action_group_set_translation_domain (actions, GETTEXT_PACKAGE);

184 185 186 187 188 189 190 191 192 193 194
	gtk_action_group_add_actions (actions, action_entries, n_action_entries, plugin);

	ui = planner_window_get_ui_manager (main_window);
	gtk_ui_manager_insert_action_group (ui, actions, 0);

	if (!gtk_ui_manager_add_ui_from_file (ui, DATADIR"/planner/ui/xml-planner-plugin.ui", &error)) {
		g_message ("Building menu failed: %s", error->message);
		g_message ("Couldn't load: %s",DATADIR"/planner/ui/xml-planner-plugin.ui");
		g_error_free (error);
	}
	gtk_ui_manager_ensure_update(ui);
195 196 197 198 199 200
}

G_MODULE_EXPORT void 
plugin_exit (PlannerPlugin *plugin) 
{
}