planner-msp-plugin.c 7.46 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2005 Imendio AB
 *
 * 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
Josué Abarca's avatar
Josué Abarca committed
17 18
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
19 20 21 22 23 24 25 26 27 28 29
 */
#include <config.h>
#include <string.h>
#include <unistd.h>
#include <libxslt/xslt.h>
#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <libexslt/exslt.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
30
#include "libplanner/mrp-paths.h"
31 32 33 34 35 36 37
#include "planner-conf.h"
#include "planner-window.h"
#include "planner-application.h"
#include "planner-plugin.h"

#define CONF_MSP_PLUGIN_LAST_DIR "/plugins/msp_plugin/last_dir"

38 39 40 41
struct _PlannerPluginPriv {
	GtkActionGroup    *actions;
};

42 43
static void msp_plugin_open (GtkAction     *action,
			     gpointer       user_data);
44 45
void        plugin_init     (PlannerPlugin *plugin);
void        plugin_exit     (PlannerPlugin *plugin);
46 47


48
static const GtkActionEntry entries[] = {
49 50 51 52 53
	{ "MspOpen",  NULL,  N_("MS Project XML..."), NULL, N_("Import an MS Project XML file"),
	  G_CALLBACK (msp_plugin_open)
	}
};

54
static gboolean
55 56 57 58 59
xml_validate (xmlDoc *doc, const gchar *dtd_path)
{
	xmlValidCtxt  cvp;
	xmlDtd       *dtd;
	gboolean      ret_val;
60

61 62 63 64
	memset (&cvp, 0, sizeof (cvp));
	dtd = xmlParseDTD (NULL, dtd_path);
	ret_val = xmlValidateDtd (&cvp, doc, dtd);
	xmlFreeDtd (dtd);
65

66 67 68 69 70 71 72 73 74 75 76 77 78 79
        return ret_val;
}

static gboolean
msp_plugin_transform (PlannerPlugin *plugin,
		      const gchar   *input_filename)
{
        xsltStylesheet *stylesheet;
        xmlDoc         *doc;
        xmlDoc         *final_doc;
	gint            fd;
	FILE           *file;
	gchar          *tmp_name, *uri;
	MrpProject     *project;
80
	gchar          *filename;
81 82 83 84 85 86

        /* libxml housekeeping */
        xmlSubstituteEntitiesDefault (1);
        xmlLoadExtDtdDefaultValue = 1;
        exsltRegisterAll ();

87 88 89
	filename = mrp_paths_get_stylesheet_dir ("msp2planner.xsl");
        stylesheet = xsltParseStylesheetFile (filename);
	g_free (filename);
90 91 92 93 94 95

	doc = xmlParseFile (input_filename);
	if (!doc) {
		xsltFreeStylesheet (stylesheet);
		return FALSE;
	}
96

97 98 99 100 101 102 103 104
        final_doc = xsltApplyStylesheet (stylesheet, doc, NULL);
        xmlFree (doc);

	if (!final_doc) {
		xsltFreeStylesheet (stylesheet);
		return FALSE;
	}

105 106
	filename = mrp_paths_get_dtd_dir ("mrproject-0.6.dtd");
	if (!xml_validate (final_doc, filename)) {
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
		GtkWidget *dialog;

		xsltFreeStylesheet (stylesheet);
		xmlFree (final_doc);

		dialog = gtk_message_dialog_new (GTK_WINDOW (plugin->main_window),
						 GTK_DIALOG_MODAL |
						 GTK_DIALOG_DESTROY_WITH_PARENT,
						 GTK_MESSAGE_ERROR,
						 GTK_BUTTONS_OK,
						 _("Couldn't import file."));
		gtk_widget_show (dialog);

		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
122 123
		g_free (filename);

124 125 126
		return FALSE;
	}

127
	g_free (filename);
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
	fd = g_file_open_tmp ("planner-msp-XXXXXX", &tmp_name, NULL);
	if (fd == -1) {
		xsltFreeStylesheet (stylesheet);
		xmlFree (final_doc);
		return FALSE;
	}

	file = fdopen (fd, "w");
	if (file == NULL) {
		xsltFreeStylesheet (stylesheet);
		xmlFree (final_doc);
		close (fd);
		g_free (tmp_name);
		return FALSE;
	}
144

145 146 147 148 149 150 151 152 153 154 155 156 157 158
	if (xsltSaveResultToFile (file, final_doc, stylesheet) == -1) {
		xsltFreeStylesheet (stylesheet);
		xmlFree (final_doc);
		fclose (file);
		g_free (tmp_name);
		return FALSE;
	}

	xsltFreeStylesheet (stylesheet);
        xmlFree (final_doc);

	uri = g_filename_to_uri (tmp_name, NULL, NULL);
	if (uri) {
		planner_window_open_in_existing_or_new (plugin->main_window, uri, TRUE);
159

160 161 162 163 164 165
		project = planner_window_get_project (plugin->main_window) ;
		mrp_project_set_uri (project, NULL);
	}

	unlink (tmp_name);
	fclose (file);
166

167 168 169 170 171 172 173 174 175 176
	g_free (tmp_name);
	g_free (uri);

	return TRUE;
}

static gchar *
msp_plugin_get_last_dir (PlannerPlugin *plugin)
{
	gchar *dir;
177

178 179
	dir = planner_conf_get_string (CONF_MSP_PLUGIN_LAST_DIR, NULL);
	if (dir == NULL) {
180
		dir = g_strdup (g_get_user_special_dir (G_USER_DIRECTORY_DOCUMENTS));
181
	}
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	return dir;
}

static void
msp_plugin_set_last_dir (PlannerPlugin *plugin,
			 const gchar   *dir)
{
	planner_conf_set_string (CONF_MSP_PLUGIN_LAST_DIR, dir, NULL);
}

static gchar *
msp_plugin_get_filename (PlannerPlugin *plugin)
{
	PlannerWindow     *window;
	GtkWidget         *file_chooser;
	GtkFileFilter     *filter;
	gint               response;
	gchar             *dir;
	gchar             *filename;

	window = plugin->main_window;
204

205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	file_chooser = gtk_file_chooser_dialog_new (_("Import a File"),
						    GTK_WINDOW (window),
						    GTK_FILE_CHOOSER_ACTION_OPEN,
						    GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
						    GTK_STOCK_OPEN, GTK_RESPONSE_OK,
						    NULL);

	filter = gtk_file_filter_new ();
	gtk_file_filter_set_name (filter, _("XML Files"));
	gtk_file_filter_add_pattern (filter, "*.xml");
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (file_chooser), filter);

	filter = gtk_file_filter_new ();
	gtk_file_filter_set_name (filter, _("All Files"));
	gtk_file_filter_add_pattern (filter, "*");
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (file_chooser), filter);

	dir = msp_plugin_get_last_dir (plugin);
	gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (file_chooser), dir);
	g_free (dir);

	gtk_window_set_modal (GTK_WINDOW (file_chooser), TRUE);
	gtk_widget_show (file_chooser);

	response = gtk_dialog_run (GTK_DIALOG (file_chooser));
	gtk_widget_hide (file_chooser);

	filename = NULL;
	if (response == GTK_RESPONSE_OK) {
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_chooser));
	}
236

237 238 239 240 241 242 243
	gtk_widget_destroy (file_chooser);

	if (filename) {
		dir = g_path_get_dirname (filename);
		msp_plugin_set_last_dir (plugin, dir);
		g_free (dir);
	}
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	return filename;
}

static void
msp_plugin_open (GtkAction *action,
		 gpointer   user_data)
{
	PlannerPlugin *plugin = user_data;
	gchar         *filename;

	filename = msp_plugin_get_filename (plugin);
	if (filename) {
		msp_plugin_transform (plugin, filename);
	}
	g_free (filename);
}

262
G_MODULE_EXPORT void
263
plugin_exit (PlannerPlugin *plugin)
264
{
265 266 267 268 269 270 271 272 273 274
	PlannerPluginPriv *priv;
	GtkUIManager   *ui;

	priv = plugin->priv;

	ui = planner_window_get_ui_manager (plugin->main_window);
	gtk_ui_manager_remove_action_group (ui, priv->actions);
	g_object_unref (priv->actions);

	g_free (priv);
275 276
}

277
G_MODULE_EXPORT void
278
plugin_init (PlannerPlugin *plugin)
279
{
280 281 282 283 284 285
	PlannerPluginPriv *priv;
	GtkUIManager      *ui;
	gchar             *filename;

	priv = g_new0 (PlannerPluginPriv, 1);
	plugin->priv = priv;
286

287 288
	priv->actions = gtk_action_group_new ("MSP plugin actions");
	gtk_action_group_set_translation_domain (priv->actions, GETTEXT_PACKAGE);
289

290
	gtk_action_group_add_actions (priv->actions,
291 292 293 294
				      entries,
				      G_N_ELEMENTS (entries),
				      plugin);

295 296
	ui = planner_window_get_ui_manager (plugin->main_window);
	gtk_ui_manager_insert_action_group (ui, priv->actions, 0);
297

298 299 300
	filename = mrp_paths_get_ui_dir ("msp-plugin.ui");
	gtk_ui_manager_add_ui_from_file (ui, filename, NULL);
	g_free (filename);
301

302 303 304
	gtk_ui_manager_ensure_update (ui);
}