2 * project-import.c (c) 2005 Johannes Schmid
3 * 2008 Ignacio Casal Quinteiro
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "project-import.h"
21 #include <libgnomevfs/gnome-vfs.h>
22 #include <libanjuta/interfaces/ianjuta-file-loader.h>
23 #include <libanjuta/anjuta-debug.h>
27 #include <gbf/gbf-backend.h>
29 #define AM_PROJECT_FILE PACKAGE_DATA_DIR"/project/terminal/project.anjuta"
30 #define MKFILE_PROJECT_FILE PACKAGE_DATA_DIR"/project/mkfile/project.anjuta"
32 static GObjectClass
*parent_class
= NULL
;
35 on_import_cancel (GtkAssistant
* assistant
, ProjectImport
* pi
)
37 g_object_unref (G_OBJECT(pi
));
41 on_import_key_press_event(GtkWidget
*widget
, GdkEventKey
*event
,
44 if (event
->keyval
== GDK_Escape
)
46 g_object_unref(G_OBJECT(pi
));
53 on_import_next(GtkAssistant
*assistant
, GtkWidget
*page
, ProjectImport
*pi
)
56 GbfBackend
* backend
= NULL
;
59 if (page
!= pi
->import_finish
)
62 const gchar
* name
= gtk_entry_get_text (GTK_ENTRY (pi
->import_name
));
63 gchar
* path
= gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (pi
->import_path
));
65 if (!name
|| !path
|| !strlen(name
) || !strlen(path
))
73 for (l
= gbf_backend_get_backends (); l
; l
= l
->next
) {
77 g_warning("Backend appears empty!");
81 /* Probe the backend to find out if the project directory is OK */
82 /* If probe() returns TRUE then we have a valid backend */
84 proj
= gbf_backend_new_project(backend
->id
);
87 if (gbf_project_probe(proj
, path
, NULL
))
89 /* This is a valid backend for this root directory */
90 /* FIXME: Possibility of more than one valid backend? */
101 gchar
* message_text
=
102 g_strdup_printf(_("Could not find a valid project backend for the "
103 "directory given (%s). Please select a different "
104 "directory, or try upgrading to a newer version of "
105 "the Gnome Build Framework."), path
);
108 GTK_DIALOG(gtk_message_dialog_new(GTK_WINDOW(pi
->assistant
),
109 GTK_DIALOG_DESTROY_WITH_PARENT
,
114 g_free(message_text
);
116 gtk_dialog_run(message
);
117 gtk_widget_destroy(GTK_WIDGET(message
));
123 gtk_label_set_text (GTK_LABEL (pi
->import_finish
), _("Please, fix the configuration"));
124 gtk_assistant_set_page_complete (GTK_ASSISTANT (pi
->assistant
), pi
->import_finish
, FALSE
);
130 summary
= g_strdup_printf(_("Project name: %s\n"
132 "Project path: %s\n"),
133 name
, backend
->name
, path
);
134 gtk_label_set_text (GTK_LABEL (pi
->import_finish
),
140 * If we are here, everything is ok
142 gtk_assistant_set_page_complete (GTK_ASSISTANT (pi
->assistant
), pi
->import_finish
, TRUE
);
145 g_free(pi
->backend_id
);
146 pi
->backend_id
= g_strdup(backend
->id
);
151 on_import_apply (GtkAssistant
*assistant
, ProjectImport
* pi
)
153 const gchar
* name
= gtk_entry_get_text (GTK_ENTRY(pi
->import_name
));
154 gchar
* path
= gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(pi
->import_path
));
156 gchar
* project_file
= g_strconcat (path
, "/", name
, ".", "anjuta", NULL
);
158 IAnjutaFileLoader
* loader
;
160 if (!project_import_generate_file (pi
, project_file
))
162 g_free (project_file
);
167 loader
= anjuta_shell_get_interface (ANJUTA_PLUGIN (pi
->plugin
)->shell
,
168 IAnjutaFileLoader
, NULL
);
171 g_warning("No IAnjutaFileLoader interface! Cannot open project file!");
172 g_free (project_file
);
175 ianjuta_file_loader_load (loader
, project_file
, FALSE
, NULL
);
176 g_free (project_file
);
181 create_start_page (ProjectImport
*pi
)
183 GtkWidget
*box
, *label
;
185 box
= gtk_hbox_new (FALSE
, 12);
186 gtk_widget_show (box
);
187 gtk_container_set_border_width (GTK_CONTAINER (box
), 12);
189 label
= gtk_label_new (_("This assistant will import an existing project into Anjuta."));
190 gtk_widget_show (label
);
191 gtk_box_pack_start (GTK_BOX (box
), label
, FALSE
, FALSE
, 0);
193 gtk_assistant_append_page (GTK_ASSISTANT (pi
->assistant
), box
);
194 gtk_assistant_set_page_title (GTK_ASSISTANT (pi
->assistant
), box
, _("Import Project"));
195 gtk_assistant_set_page_complete (GTK_ASSISTANT (pi
->assistant
), box
, TRUE
);
196 gtk_assistant_set_page_type (GTK_ASSISTANT (pi
->assistant
), box
, GTK_ASSISTANT_PAGE_INTRO
);
200 on_entry_changed (GtkWidget
*widget
, gpointer data
)
202 GtkAssistant
*assistant
= GTK_ASSISTANT (data
);
203 GtkWidget
*current_page
;
207 page_number
= gtk_assistant_get_current_page (assistant
);
208 current_page
= gtk_assistant_get_nth_page (assistant
, page_number
);
209 text
= gtk_entry_get_text (GTK_ENTRY (widget
));
212 gtk_assistant_set_page_complete (assistant
, current_page
, TRUE
);
214 gtk_assistant_set_page_complete (assistant
, current_page
, FALSE
);
218 create_import_page (ProjectImport
*pi
)
220 GtkWidget
*box
, *vbox1
, *vbox2
;
223 box
= gtk_vbox_new (FALSE
, 12);
224 gtk_widget_show (box
);
225 gtk_container_set_border_width (GTK_CONTAINER (box
), 5);
230 vbox1
= gtk_vbox_new (FALSE
, 12);
231 gtk_widget_show (vbox1
);
232 gtk_box_pack_start (GTK_BOX (box
), vbox1
, FALSE
, FALSE
, 0);
234 label
= gtk_label_new (NULL
);
235 gtk_label_set_markup (GTK_LABEL (label
),
236 _("<b>Enter the project name:</b>"));
237 gtk_widget_show (label
);
238 gtk_box_pack_start (GTK_BOX (vbox1
), label
, FALSE
, FALSE
, 0);
240 pi
->import_name
= gtk_entry_new ();
241 gtk_widget_show (pi
->import_name
);
242 gtk_box_pack_start (GTK_BOX (vbox1
), pi
->import_name
, FALSE
, FALSE
, 0);
243 g_signal_connect (G_OBJECT (pi
->import_name
), "changed",
244 G_CALLBACK (on_entry_changed
), pi
->assistant
);
249 vbox2
= gtk_vbox_new (FALSE
, 12);
250 gtk_widget_show (vbox2
);
251 gtk_box_pack_start (GTK_BOX (box
), vbox2
, FALSE
, FALSE
, 0);
253 label
= gtk_label_new (NULL
);
254 gtk_label_set_markup (GTK_LABEL (label
),
255 _("<b>Enter the base path of your project:</b>"));
256 gtk_widget_show (label
);
257 gtk_box_pack_start (GTK_BOX (vbox2
), label
, FALSE
, FALSE
, 0);
259 pi
->import_path
= gtk_file_chooser_button_new (_("Select project directory"),
260 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER
);
261 gtk_widget_show (pi
->import_path
);
262 gtk_box_pack_start (GTK_BOX (vbox2
), pi
->import_path
, FALSE
, FALSE
, 0);
264 gtk_assistant_append_page (GTK_ASSISTANT (pi
->assistant
), box
);
265 gtk_assistant_set_page_title (GTK_ASSISTANT (pi
->assistant
), box
, _("Project to Import"));
269 create_finish_page (ProjectImport
*pi
)
271 pi
->import_finish
= gtk_label_new (NULL
);
272 gtk_widget_show (pi
->import_finish
);
274 gtk_assistant_append_page (GTK_ASSISTANT (pi
->assistant
), pi
->import_finish
);
275 gtk_assistant_set_page_type (GTK_ASSISTANT (pi
->assistant
), pi
->import_finish
,
276 GTK_ASSISTANT_PAGE_CONFIRM
);
277 gtk_assistant_set_page_complete (GTK_ASSISTANT (pi
->assistant
), pi
->import_finish
, TRUE
);
278 gtk_assistant_set_page_title (GTK_ASSISTANT (pi
->assistant
), pi
->import_finish
, _("Confirmation"));
282 project_import_init (ProjectImport
*pi
)
284 pi
->assistant
= gtk_assistant_new ();
285 create_start_page (pi
);
286 create_import_page (pi
);
287 create_finish_page (pi
);
289 pi
->backend_id
= NULL
;
291 g_signal_connect(G_OBJECT (pi
->assistant
), "prepare",
292 G_CALLBACK(on_import_next
), pi
);
293 g_signal_connect(G_OBJECT (pi
->assistant
), "apply",
294 G_CALLBACK (on_import_apply
), pi
);
295 g_signal_connect(G_OBJECT (pi
->assistant
), "cancel",
296 G_CALLBACK (on_import_cancel
), pi
);
297 g_signal_connect(G_OBJECT (pi
->assistant
), "close",
298 G_CALLBACK (on_import_cancel
), pi
);
299 g_signal_connect(G_OBJECT(pi
->assistant
), "key-press-event",
300 G_CALLBACK(on_import_key_press_event
), pi
);
302 gtk_widget_show (pi
->assistant
);
306 project_import_finalize(GObject
*object
)
309 cobj
= PROJECT_IMPORT(object
);
311 DEBUG_PRINT ("Finalizing ProjectImport object");
313 if (cobj
->backend_id
)
314 g_free(cobj
->backend_id
);
316 gtk_widget_destroy(cobj
->assistant
);
318 /* Deactivate plugin once wizard is finished */
319 if (anjuta_plugin_is_active(cobj
->plugin
))
320 anjuta_plugin_deactivate (cobj
->plugin
);
321 G_OBJECT_CLASS(parent_class
)->finalize(object
);
325 project_import_class_init(ProjectImportClass
*klass
)
327 GObjectClass
*object_class
= G_OBJECT_CLASS(klass
);
329 parent_class
= g_type_class_peek_parent(klass
);
330 object_class
->finalize
= project_import_finalize
;
334 project_import_get_type()
336 static GType type
= 0;
339 static const GTypeInfo our_info
= {
340 sizeof (ProjectImportClass
),
343 (GClassInitFunc
)project_import_class_init
,
346 sizeof (ProjectImport
),
348 (GInstanceInitFunc
)project_import_init
,
351 type
= g_type_register_static(G_TYPE_OBJECT
,
352 "ProjectImport", &our_info
, 0);
359 project_import_new(AnjutaPlugin
* plugin
)
363 obj
= PROJECT_IMPORT(g_object_new(PROJECT_IMPORT_TYPE
, NULL
));
365 obj
->plugin
= plugin
;
366 gtk_window_set_transient_for (GTK_WINDOW (obj
->assistant
),
367 GTK_WINDOW (ANJUTA_PLUGIN (plugin
)->shell
));
373 project_import_set_name (ProjectImport
*pi
, const gchar
*name
)
375 g_return_if_fail (IS_PROJECT_IMPORT (pi
));
376 g_return_if_fail (name
!= NULL
);
378 gtk_entry_set_text (GTK_ENTRY (pi
->import_name
), name
);
382 project_import_set_directory (ProjectImport
*pi
, const gchar
*directory
)
384 g_return_if_fail (IS_PROJECT_IMPORT (pi
));
385 g_return_if_fail (directory
!= NULL
);
387 gchar
* uri
= gnome_vfs_make_uri_from_input (directory
);
388 gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (pi
->import_path
), uri
);
393 project_import_generate_file(ProjectImport
* pi
, const gchar
* prjfile
)
395 /* Of course we could do some more intelligent stuff here
396 and check which plugins are really needed but for now we just
397 take a default project file. */
399 GnomeVFSURI
* source_uri
;
400 if (!strcmp (pi
->backend_id
, "gbf-am:GbfAmProject"))
401 source_uri
= gnome_vfs_uri_new(AM_PROJECT_FILE
);
402 else if (!strcmp (pi
->backend_id
, "gbf-mkfile:GbfMkfileProject"))
403 source_uri
= gnome_vfs_uri_new(MKFILE_PROJECT_FILE
);
406 /* We shouldn't get here, unless someone has upgraded their GBF */
407 /* but not Anjuta. */
411 dlg
= gtk_message_dialog_new(GTK_WINDOW(pi
->assistant
),
412 GTK_DIALOG_DESTROY_WITH_PARENT
,
415 _("Generation of project file failed. Cannot "
416 "find an appropriate project template to "
417 "use. Please make sure your version of "
418 "Anjuta is up to date."));
420 gtk_dialog_run(GTK_DIALOG(dlg
));
421 gtk_widget_destroy (dlg
);
425 GnomeVFSURI
* dest_uri
= gnome_vfs_uri_new(prjfile
);
427 GnomeVFSResult error
= gnome_vfs_xfer_uri (source_uri
,
429 GNOME_VFS_XFER_DEFAULT
,
430 GNOME_VFS_XFER_ERROR_MODE_ABORT
,
431 GNOME_VFS_XFER_OVERWRITE_MODE_ABORT
,
434 /* Handle already existing file */
435 if (error
== GNOME_VFS_ERROR_FILE_EXISTS
)
437 if (anjuta_util_dialog_boolean_question (GTK_WINDOW (pi
->assistant
),
438 _("A file named \"%s\" already exists. "
439 "Do you want to replace it?"), prjfile
))
441 error
= gnome_vfs_xfer_uri (source_uri
,
443 GNOME_VFS_XFER_DEFAULT
,
444 GNOME_VFS_XFER_ERROR_MODE_ABORT
,
445 GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE
,
451 /* Update file time if possible */
452 if (error
== GNOME_VFS_OK
)
454 GnomeVFSFileInfo
*file_info
;
456 file_info
= gnome_vfs_file_info_new ();
457 file_info
->ctime
= time (NULL
);
458 file_info
->mtime
= file_info
->ctime
;
459 file_info
->atime
= file_info
->ctime
;
461 gnome_vfs_set_file_info_uri (dest_uri
,
463 GNOME_VFS_SET_FILE_INFO_TIME
);
465 gnome_vfs_file_info_unref (file_info
);
468 gnome_vfs_uri_unref (source_uri
);
469 gnome_vfs_uri_unref (dest_uri
);
475 case GNOME_VFS_ERROR_FILE_EXISTS
:
478 anjuta_util_dialog_error (GTK_WINDOW (pi
->assistant
),
479 _("A file named \"%s\" cannot be written: %s. "
480 "Check if you have write access to the project directory."),
481 prjfile
, gnome_vfs_result_to_string (error
));