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_slist_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_slist_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)
158 GtkIconSource *source;
159 GtkIconSet *icon_set;
160 GtkIconFactory *factory;
161 factory=gtk_icon_factory_new();
162 icon_set=gtk_icon_set_new();
163 if (pixbuf_supports_svg())
165 source=gtk_icon_source_new();
166 s=g_build_filename(prefix?prefix:"/usr",
167 "share/icons/hicolor/scalable/apps/plover-applications.svg",NULL);
168 gtk_icon_source_set_filename(source,s);
170 gtk_icon_set_add_source(icon_set,source);
171 gtk_icon_source_free(source);
175 s=g_build_filename(prefix?prefix:"/usr",
176 "share/icons/hicolor/24x24/apps/plover-applications.png",NULL);
177 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_MENU,s);
178 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_BUTTON,s);
179 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR,s);
180 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR,s);
182 s=g_build_filename(prefix?prefix:"/usr",
183 "share/icons/hicolor/48x48/apps/plover-applications.png",NULL);
184 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_DND,s);
185 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_DIALOG,s);
188 gtk_icon_factory_add(factory,LOGO_NAME,icon_set);
189 gtk_icon_set_unref(icon_set);
190 icon_set=gtk_icon_factory_lookup(factory,LOGO_NAME);
191 gtk_icon_factory_add_default(factory);
192 g_object_unref(factory);
193 icon_set=gtk_icon_factory_lookup_default(LOGO_NAME);
194 gtk_window_set_default_icon_name(LOGO_NAME);
197 int main(int argc,char **argv)
203 PloverPackageSet *set;
204 GOptionEntry options[]={
207 if (!gtk_init_with_args(&argc,&argv,NULL,options,NULL,&err))
209 g_printerr("%s",err->message);
213 prefix=g_win32_get_package_installation_directory_of_module(NULL);
216 ui=gtk_builder_new();
217 if (!g_file_get_contents("app-manager.ui",&contents,&len,&err) &&
218 g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
221 s=g_build_filename(prefix,"share","plover","app-manager.ui",NULL);
223 s=g_build_filename(PLOVER_DATADIR,"app-manager.ui",NULL);
226 (void)g_file_get_contents(s,&contents,&len,&err);
231 (void)gtk_builder_add_from_string(ui,contents,len,&err);
236 g_error("%s",err->message);
241 relocations=razor_relocations_create();
242 razor_relocations_add(relocations,"/usr",prefix);
244 gtk_builder_connect_signals(ui,NULL);
245 gtk_link_button_set_uri_hook(show_uri,NULL,NULL);
246 installed=GTK_TREE_MODEL(plover_package_store_new());
247 set=plover_package_set_new_from_installed("",NULL);
250 plover_package_store_add_set(PLOVER_PACKAGE_STORE(installed),set);
251 if (plover_package_set_get_no_details(set))
253 w=GTK_WIDGET(gtk_builder_get_object(ui,"ViewFiles"));
254 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w),TRUE);
257 applications=plover_applications_model_new(installed);
258 set_package_model(applications);
262 razor_relocations_destroy(relocations);
268 on_applications_toggled(GtkToggleToolButton *button,gpointer data)
270 if (gtk_toggle_tool_button_get_active(button))
273 applications=plover_applications_model_new(installed);
274 set_package_model(applications);
279 on_all_packages_toggled(GtkToggleToolButton *button,gpointer data)
281 if (gtk_toggle_tool_button_get_active(button))
282 set_package_model(installed);
286 on_local_media_toggled(GtkToggleToolButton *button,gpointer data)
288 if (gtk_toggle_tool_button_get_active(button))
292 show_busy_cursor(TRUE);
293 local_media=plover_local_media_store_new();
294 show_busy_cursor(FALSE);
296 set_package_model(local_media);
301 on_location_toggled(GtkToggleToolButton *button,gpointer data)
303 if (gtk_toggle_tool_button_get_active(button))
304 set_package_model(location);
307 G_MODULE_EXPORT void on_open_location(GtkWidget *widget)
309 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow"));
315 PloverPackageSet *set;
318 dialog=gtk_file_chooser_dialog_new("Open Location",GTK_WINDOW(w),
319 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,GTK_STOCK_CANCEL,
320 GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL);
321 #if GTK_CHECK_VERSION(2,18,0)
322 gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog),FALSE);
324 if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_ACCEPT)
326 show_busy_cursor(TRUE);
327 path=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
328 set=plover_package_set_new_from_repository(path,relocations,&err);
332 location=GTK_TREE_MODEL(plover_package_store_new());
334 plover_package_store_get_sets(PLOVER_PACKAGE_STORE(location))))
335 plover_package_store_remove_set(PLOVER_PACKAGE_STORE(location),
336 PLOVER_PACKAGE_SET(sets->data));
337 plover_package_store_add_set(PLOVER_PACKAGE_STORE(location),set);
339 w=GTK_WIDGET(gtk_builder_get_object(ui,"LocationButton"));
340 file=g_file_new_for_path(path);
341 parent=g_file_get_parent(file);
344 g_object_unref(parent);
348 mount=g_file_find_enclosing_mount(file,NULL,NULL);
351 name=g_mount_get_name(mount);
352 g_object_unref(mount);
356 fi=g_file_query_info(file,
357 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
358 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,NULL,NULL);
361 name=g_strdup(g_file_info_get_display_name(fi));
365 name=g_filename_display_basename(path);
366 g_object_unref(file);
368 gtk_tool_button_set_label(GTK_TOOL_BUTTON(w),name);
371 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(w),TRUE);
375 gtk_widget_destroy(dialog);
376 dialog=gtk_message_dialog_new(GTK_WINDOW(w),
377 GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR,
378 GTK_BUTTONS_CLOSE,"Error loading repository '%s'",path);
379 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
381 gtk_dialog_run(GTK_DIALOG(dialog));
385 show_busy_cursor(FALSE);
387 gtk_widget_destroy(dialog);
390 G_MODULE_EXPORT void on_scan_local_media(GtkWidget *widget)
392 show_busy_cursor(TRUE);
394 local_media=plover_local_media_store_new();
395 plover_local_media_store_scan(PLOVER_LOCAL_MEDIA_STORE(local_media));
396 show_busy_cursor(FALSE);
399 G_MODULE_EXPORT void on_help_about(GtkWidget *widget)
401 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow"));
402 gtk_show_about_dialog(GTK_WINDOW(w),"name",PACKAGE_NAME,
403 "version",PACKAGE_VERSION,"comments","Application Manager",
404 "copyright","Copyright © 2010 J. Ali Harlow","logo-icon-name",LOGO_NAME,
408 G_MODULE_EXPORT void on_find_clicked(GtkButton *button)
411 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"SearchEntry"));
412 text=g_strdup(gtk_entry_get_text(GTK_ENTRY(w)));
413 gtk_entry_set_text(GTK_ENTRY(w),"");
414 gtk_entry_set_text(GTK_ENTRY(w),text);