2 * Copyright (C) 2010 J. Ali Harlow <ali@juiblex.co.uk>
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.
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.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <plover-gtk/packageset.h>
26 #include "app-manager.h"
27 #include "localmedia.h"
29 #define LOGO_NAME "plover-applications"
32 GtkTreeModel *installed,*applications,*location,*local_media;
34 struct razor_relocations *relocations=NULL;
36 void show_busy_cursor(gboolean busy)
38 GList *list,*link,*remaining;
42 list=gtk_window_list_toplevels();
45 w=GTK_WIDGET(list->data);
49 list=g_list_remove_link(list,link);
54 display=gtk_widget_get_display(w);
55 cursor=busy?gdk_cursor_new_for_display(display,GDK_WATCH):NULL;
57 for(link=list;link;link=link->next)
59 w=GTK_WIDGET(link->data);
62 if (gtk_widget_get_display(w)==display)
63 gdk_window_set_cursor(w->window,cursor);
65 remaining=g_list_prepend(remaining,w);
68 gdk_display_flush(display);
70 gdk_cursor_unref(cursor);
78 * In Gtk+ 2.16.6, the default handler generates g_warnings on error.
79 * It should display an error to the user. Do it ourselves.
82 static void show_uri(GtkLinkButton *button,const gchar *uri,gpointer data)
87 if (gtk_widget_has_screen(GTK_WIDGET(button)))
88 screen=gtk_widget_get_screen(GTK_WIDGET(button));
91 gtk_show_uri(screen,uri,GDK_CURRENT_TIME,&error);
94 dialog=gtk_message_dialog_new(
95 GTK_WINDOW(gtk_builder_get_object(ui,"MainWindow")),
96 GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,
97 "Unable to show '%s'",uri);
98 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
101 gtk_dialog_run(GTK_DIALOG(dialog));
102 gtk_widget_destroy(dialog);
106 /* Checks whether a loader for SVG files has been registered
109 static gboolean pixbuf_supports_svg(void)
113 static gint found_svg=-1;
114 gchar **mime_types,**mime_type;
117 formats=gdk_pixbuf_get_formats();
119 for (tmp_list=formats;tmp_list && !found_svg;tmp_list=tmp_list->next)
121 mime_types=gdk_pixbuf_format_get_mime_types(tmp_list->data);
122 for (mime_type=mime_types;*mime_type && !found_svg;mime_type++)
123 if (!strcmp(*mime_type,"image/svg"))
125 g_strfreev(mime_types);
127 g_slist_free(formats);
131 static void install_icon_at_size(const char *icon_name,GtkIconSet *icon_set,
132 GtkIconSize size,const char *filename)
136 GtkIconSource *source;
137 if (gtk_icon_size_lookup(size,&w,&h))
139 pixbuf=gdk_pixbuf_new_from_file_at_size(filename,w,h,NULL);
142 source=gtk_icon_source_new();
143 gtk_icon_source_set_size_wildcarded(source,FALSE);
144 gtk_icon_source_set_size(source,size);
145 gtk_icon_source_set_pixbuf(source,pixbuf);
146 gtk_icon_set_add_source(icon_set,source);
147 gtk_icon_source_free(source);
148 g_object_unref(pixbuf);
153 static void install_icons(void)
156 GtkIconSource *source;
157 GtkIconSet *icon_set;
158 GtkIconFactory *factory;
159 factory=gtk_icon_factory_new();
160 icon_set=gtk_icon_set_new();
161 if (pixbuf_supports_svg())
163 source=gtk_icon_source_new();
164 s=g_build_filename(prefix?prefix:"/usr",
165 "share/icons/hicolor/scalable/apps/plover-applications.svg",NULL);
166 gtk_icon_source_set_filename(source,s);
168 gtk_icon_set_add_source(icon_set,source);
169 gtk_icon_source_free(source);
173 s=g_build_filename(prefix?prefix:"/usr",
174 "share/icons/hicolor/24x24/apps/plover-applications.png",NULL);
175 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_MENU,s);
176 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_BUTTON,s);
177 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR,s);
178 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR,s);
180 s=g_build_filename(prefix?prefix:"/usr",
181 "share/icons/hicolor/48x48/apps/plover-applications.png",NULL);
182 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_DND,s);
183 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_DIALOG,s);
186 gtk_icon_factory_add(factory,LOGO_NAME,icon_set);
187 gtk_icon_set_unref(icon_set);
188 icon_set=gtk_icon_factory_lookup(factory,LOGO_NAME);
189 gtk_icon_factory_add_default(factory);
190 g_object_unref(factory);
191 icon_set=gtk_icon_factory_lookup_default(LOGO_NAME);
192 gtk_window_set_default_icon_name(LOGO_NAME);
195 int main(int argc,char **argv)
201 PloverPackageSet *set;
202 GOptionEntry options[]={
205 if (!gtk_init_with_args(&argc,&argv,NULL,options,NULL,&err))
207 g_printerr("%s",err->message);
211 prefix=g_win32_get_package_installation_directory_of_module(NULL);
214 ui=gtk_builder_new();
215 if (!g_file_get_contents("app-manager.ui",&contents,&len,&err) &&
216 g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
219 s=g_build_filename(prefix,"share","plover","app-manager.ui",NULL);
221 s=g_build_filename(PLOVER_DATADIR,"app-manager.ui",NULL);
224 (void)g_file_get_contents(s,&contents,&len,&err);
229 (void)gtk_builder_add_from_string(ui,contents,len,&err);
234 g_error("%s",err->message);
239 relocations=razor_relocations_create();
240 razor_relocations_add(relocations,"/usr",prefix);
242 gtk_builder_connect_signals(ui,NULL);
243 gtk_link_button_set_uri_hook(show_uri,NULL,NULL);
244 installed=GTK_TREE_MODEL(plover_package_store_new());
245 set=plover_package_set_new_from_installed("",NULL);
248 plover_package_store_add_set(PLOVER_PACKAGE_STORE(installed),set);
249 if (plover_package_set_get_no_details(set))
251 w=GTK_WIDGET(gtk_builder_get_object(ui,"ViewFiles"));
252 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w),TRUE);
255 applications=plover_applications_model_new(installed);
256 set_package_model(applications);
260 razor_relocations_destroy(relocations);
266 on_applications_toggled(GtkToggleToolButton *button,gpointer data)
268 if (gtk_toggle_tool_button_get_active(button))
271 applications=plover_applications_model_new(installed);
272 set_package_model(applications);
277 on_all_packages_toggled(GtkToggleToolButton *button,gpointer data)
279 if (gtk_toggle_tool_button_get_active(button))
280 set_package_model(installed);
284 on_local_media_toggled(GtkToggleToolButton *button,gpointer data)
286 if (gtk_toggle_tool_button_get_active(button))
290 show_busy_cursor(TRUE);
291 local_media=plover_local_media_store_new();
292 show_busy_cursor(FALSE);
294 set_package_model(local_media);
299 on_location_toggled(GtkToggleToolButton *button,gpointer data)
301 if (gtk_toggle_tool_button_get_active(button))
302 set_package_model(location);
305 G_MODULE_EXPORT void on_open_location(GtkWidget *widget)
307 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow"));
313 PloverPackageSet *set;
316 dialog=gtk_file_chooser_dialog_new("Open Location",GTK_WINDOW(w),
317 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,GTK_STOCK_CANCEL,
318 GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL);
319 #if GTK_CHECK_VERSION(2,18,0)
320 gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog),FALSE);
322 if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_ACCEPT)
324 show_busy_cursor(TRUE);
325 path=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
326 set=plover_package_set_new_from_repository(path,relocations,&err);
330 location=GTK_TREE_MODEL(plover_package_store_new());
332 plover_package_store_get_sets(PLOVER_PACKAGE_STORE(location))))
333 plover_package_store_remove_set(PLOVER_PACKAGE_STORE(location),
334 PLOVER_PACKAGE_SET(sets->data));
335 plover_package_store_add_set(PLOVER_PACKAGE_STORE(location),set);
337 w=GTK_WIDGET(gtk_builder_get_object(ui,"LocationButton"));
338 file=g_file_new_for_path(path);
339 parent=g_file_get_parent(file);
342 g_object_unref(parent);
346 mount=g_file_find_enclosing_mount(file,NULL,NULL);
349 name=g_mount_get_name(mount);
350 g_object_unref(mount);
354 fi=g_file_query_info(file,
355 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
356 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,NULL,NULL);
359 name=g_strdup(g_file_info_get_display_name(fi));
363 name=g_filename_display_basename(path);
364 g_object_unref(file);
366 gtk_tool_button_set_label(GTK_TOOL_BUTTON(w),name);
369 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(w),TRUE);
373 gtk_widget_destroy(dialog);
374 dialog=gtk_message_dialog_new(GTK_WINDOW(w),
375 GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR,
376 GTK_BUTTONS_CLOSE,"Error loading repository '%s'",path);
377 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
379 gtk_dialog_run(GTK_DIALOG(dialog));
383 show_busy_cursor(FALSE);
385 gtk_widget_destroy(dialog);
388 G_MODULE_EXPORT void on_scan_local_media(GtkWidget *widget)
390 show_busy_cursor(TRUE);
392 local_media=plover_local_media_store_new();
393 plover_local_media_store_scan(PLOVER_LOCAL_MEDIA_STORE(local_media));
394 show_busy_cursor(FALSE);
397 G_MODULE_EXPORT void on_help_about(GtkWidget *widget)
399 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow"));
400 gtk_show_about_dialog(GTK_WINDOW(w),"name",PACKAGE_NAME,
401 "version",PACKAGE_VERSION,"comments","Application Manager",
402 "copyright","Copyright © 2010 J. Ali Harlow","logo-icon-name",LOGO_NAME,
406 G_MODULE_EXPORT void on_find_clicked(GtkButton *button)
409 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"SearchEntry"));
410 text=g_strdup(gtk_entry_get_text(GTK_ENTRY(w)));
411 gtk_entry_set_text(GTK_ENTRY(w),"");
412 gtk_entry_set_text(GTK_ENTRY(w),text);