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.
29 #include <whelk/whelk.h>
30 #include <plover/packageset.h>
31 #include <plover-gtk/stockicons.h>
32 #include "app-manager.h"
33 #include "localmedia.h"
35 LUALIB_API int luaopen_posix(lua_State *L);
37 #define LOGO_NAME "plover-applications"
40 GtkTreeModel *installed,*applications,*location,*local_media;
42 struct razor_relocations *relocations=NULL;
44 void show_busy_cursor(gboolean busy)
46 GList *list,*link,*remaining;
50 list=gtk_window_list_toplevels();
53 w=GTK_WIDGET(list->data);
57 list=g_list_remove_link(list,link);
62 display=gtk_widget_get_display(w);
63 cursor=busy?gdk_cursor_new_for_display(display,GDK_WATCH):NULL;
65 for(link=list;link;link=link->next)
67 w=GTK_WIDGET(link->data);
70 if (gtk_widget_get_display(w)==display)
71 gdk_window_set_cursor(w->window,cursor);
73 remaining=g_list_prepend(remaining,w);
76 gdk_display_flush(display);
78 gdk_cursor_unref(cursor);
86 * In Gtk+ 2.16.6, the default handler generates g_warnings on error.
87 * It should display an error to the user. Do it ourselves.
90 static void show_uri(GtkLinkButton *button,const gchar *uri,gpointer data)
95 if (gtk_widget_has_screen(GTK_WIDGET(button)))
96 screen=gtk_widget_get_screen(GTK_WIDGET(button));
99 gtk_show_uri(screen,uri,GDK_CURRENT_TIME,&error);
102 dialog=gtk_message_dialog_new(
103 GTK_WINDOW(gtk_builder_get_object(ui,"MainWindow")),
104 GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,
105 "Unable to show '%s'",uri);
106 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
109 gtk_dialog_run(GTK_DIALOG(dialog));
110 gtk_widget_destroy(dialog);
115 /* Checks whether a loader for SVG files has been registered
118 static gboolean pixbuf_supports_svg(void)
122 static gint found_svg=-1;
123 gchar **mime_types,**mime_type;
126 formats=gdk_pixbuf_get_formats();
128 for (tmp_list=formats;tmp_list && !found_svg;tmp_list=tmp_list->next)
130 mime_types=gdk_pixbuf_format_get_mime_types(tmp_list->data);
131 for (mime_type=mime_types;*mime_type && !found_svg;mime_type++)
132 if (!strcmp(*mime_type,"image/svg"))
134 g_strfreev(mime_types);
136 g_slist_free(formats);
140 static void install_icon_at_size(const char *icon_name,GtkIconSet *icon_set,
141 GtkIconSize size,const char *filename)
145 GtkIconSource *source;
146 if (gtk_icon_size_lookup(size,&w,&h))
148 pixbuf=gdk_pixbuf_new_from_file_at_size(filename,w,h,NULL);
151 source=gtk_icon_source_new();
152 gtk_icon_source_set_size_wildcarded(source,FALSE);
153 gtk_icon_source_set_size(source,size);
154 gtk_icon_source_set_pixbuf(source,pixbuf);
155 gtk_icon_set_add_source(icon_set,source);
156 gtk_icon_source_free(source);
157 g_object_unref(pixbuf);
162 static void install_icons(void)
165 GtkIconSource *source;
166 GtkIconSet *icon_set;
167 GtkIconFactory *factory;
168 factory=gtk_icon_factory_new();
169 icon_set=gtk_icon_set_new();
170 if (pixbuf_supports_svg())
172 source=gtk_icon_source_new();
173 s=g_build_filename(prefix?prefix:"/usr",
174 "share/icons/hicolor/scalable/apps/plover-applications.svg",NULL);
175 gtk_icon_source_set_filename(source,s);
177 gtk_icon_set_add_source(icon_set,source);
178 gtk_icon_source_free(source);
182 s=g_build_filename(prefix?prefix:"/usr",
183 "share/icons/hicolor/24x24/apps/plover-applications.png",NULL);
184 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_MENU,s);
185 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_BUTTON,s);
186 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR,s);
187 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR,s);
189 s=g_build_filename(prefix?prefix:"/usr",
190 "share/icons/hicolor/48x48/apps/plover-applications.png",NULL);
191 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_DND,s);
192 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_DIALOG,s);
195 gtk_icon_factory_add(factory,LOGO_NAME,icon_set);
196 gtk_icon_set_unref(icon_set);
197 icon_set=gtk_icon_factory_lookup(factory,LOGO_NAME);
198 gtk_icon_factory_add_default(factory);
199 g_object_unref(factory);
200 icon_set=gtk_icon_factory_lookup_default(LOGO_NAME);
201 gtk_window_set_default_icon_name(LOGO_NAME);
205 static void install_icons(void)
207 GtkIconSet *icon_set;
208 plover_icons_add_to_stock("apps",LOGO_NAME);
209 icon_set=gtk_icon_factory_lookup_default(LOGO_NAME);
210 gtk_window_set_default_icon_name(LOGO_NAME);
213 int main(int argc,char **argv)
218 gchar *setup_base=NULL,*update_base=NULL;
220 PloverPackageSet *set;
221 GSList *objects,*lnk;
223 GOptionEntry options[]={
224 {"setup",0,0,G_OPTION_ARG_FILENAME,&setup_base,
225 "Setup from installation media","path"},
226 {"update",0,0,G_OPTION_ARG_FILENAME,&update_base,
227 "Update from upgrade media","path"},
232 * app-manager is normally a GUI application, but rpm scripts may well
233 * call console applications and it looks ugly if console windows keep
234 * popping up. Avoid this by allocating our own console and hiding it.
236 * - If app-manager is a console application (typically for debugging),
237 * then skip this step.
238 * - Call ShowWindow twice to negate special handling on first call.
240 if (!GetConsoleWindow())
243 ShowWindow(GetConsoleWindow(),SW_HIDE);
244 ShowWindow(GetConsoleWindow(),SW_HIDE);
247 razor_set_lua_loader("posix",luaopen_posix);
248 razor_set_lua_loader("whelk",luaopen_whelk);
249 if (!gtk_init_with_args(&argc,&argv,NULL,options,NULL,&err))
251 g_printerr("%s\n",err->message);
254 if (setup_base && update_base)
256 g_printerr("--setup and --update are mutually exclusive\n");
260 prefix=g_win32_get_package_installation_directory_of_module(NULL);
263 ui=gtk_builder_new();
264 if (!g_file_get_contents("app-manager.ui",&contents,&len,&err) &&
265 g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
268 s=g_build_filename(prefix,"share","plover","app-manager.ui",NULL);
270 s=g_build_filename(PLOVER_DATADIR,"app-manager.ui",NULL);
273 (void)g_file_get_contents(s,&contents,&len,&err);
278 (void)gtk_builder_add_from_string(ui,contents,len,&err);
283 g_error("%s",err->message);
288 relocations=razor_relocations_create();
289 razor_relocations_add(relocations,"/usr",prefix);
291 gtk_builder_connect_signals(ui,NULL);
292 gtk_link_button_set_uri_hook(show_uri,NULL,NULL);
293 installed=GTK_TREE_MODEL(plover_package_store_new());
294 set=plover_package_set_new();
295 (void)plover_package_set_open(set,"",TRUE,NULL);
296 plover_package_store_add_set(PLOVER_PACKAGE_STORE(installed),set);
297 if (plover_package_set_get_no_details(set))
299 w=GTK_WIDGET(gtk_builder_get_object(ui,"ViewFiles"));
300 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w),TRUE);
302 applications=plover_applications_model_new(installed);
303 set_package_model(applications);
305 started=setup(set,setup_base);
306 else if (update_base)
307 started=update(set,update_base);
310 w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow"));
316 g_clear_object(&set);
317 objects=gtk_builder_get_objects(ui);
318 for(lnk=objects;lnk;lnk=lnk->next)
319 if (GTK_IS_WIDGET(lnk->data) &&
320 gtk_widget_is_toplevel(GTK_WIDGET(lnk->data)))
321 gtk_widget_destroy(GTK_WIDGET(lnk->data));
322 g_slist_free(objects);
324 g_clear_object(&installed);
325 g_clear_object(&applications);
326 g_clear_object(&location);
327 g_clear_object(&local_media);
329 razor_relocations_destroy(relocations);
337 on_applications_toggled(GtkToggleToolButton *button,gpointer data)
339 if (gtk_toggle_tool_button_get_active(button))
342 applications=plover_applications_model_new(installed);
343 set_package_model(applications);
348 on_all_packages_toggled(GtkToggleToolButton *button,gpointer data)
350 if (gtk_toggle_tool_button_get_active(button))
351 set_package_model(installed);
355 on_local_media_toggled(GtkToggleToolButton *button,gpointer data)
357 if (gtk_toggle_tool_button_get_active(button))
361 show_busy_cursor(TRUE);
362 local_media=plover_local_media_store_new();
363 show_busy_cursor(FALSE);
365 set_package_model(local_media);
370 on_location_toggled(GtkToggleToolButton *button,gpointer data)
372 if (gtk_toggle_tool_button_get_active(button))
373 set_package_model(location);
376 G_MODULE_EXPORT void on_open_location(GtkWidget *widget)
378 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow"));
384 PloverPackageSet *set;
387 dialog=gtk_file_chooser_dialog_new("Open Location",GTK_WINDOW(w),
388 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,GTK_STOCK_CANCEL,
389 GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL);
390 #if GTK_CHECK_VERSION(2,18,0)
391 gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog),FALSE);
393 if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_ACCEPT)
395 show_busy_cursor(TRUE);
396 path=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
397 set=plover_package_set_new_from_yum(path,relocations,&err);
401 location=GTK_TREE_MODEL(plover_package_store_new());
403 plover_package_store_get_sets(PLOVER_PACKAGE_STORE(location))))
404 plover_package_store_remove_set(PLOVER_PACKAGE_STORE(location),
405 PLOVER_PACKAGE_SET(sets->data));
406 plover_package_store_add_set(PLOVER_PACKAGE_STORE(location),set);
408 w=GTK_WIDGET(gtk_builder_get_object(ui,"LocationButton"));
409 file=g_file_new_for_path(path);
410 parent=g_file_get_parent(file);
413 g_object_unref(parent);
417 mount=g_file_find_enclosing_mount(file,NULL,NULL);
420 name=g_mount_get_name(mount);
421 g_object_unref(mount);
425 fi=g_file_query_info(file,
426 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
427 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,NULL,NULL);
430 name=g_strdup(g_file_info_get_display_name(fi));
434 name=g_filename_display_basename(path);
435 g_object_unref(file);
437 gtk_tool_button_set_label(GTK_TOOL_BUTTON(w),name);
440 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(w),TRUE);
444 gtk_widget_destroy(dialog);
445 dialog=gtk_message_dialog_new(GTK_WINDOW(w),
446 GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR,
447 GTK_BUTTONS_CLOSE,"Error loading repository '%s'",path);
448 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
450 gtk_dialog_run(GTK_DIALOG(dialog));
454 show_busy_cursor(FALSE);
456 gtk_widget_destroy(dialog);
459 G_MODULE_EXPORT void on_scan_local_media(GtkWidget *widget)
461 show_busy_cursor(TRUE);
463 local_media=plover_local_media_store_new();
464 plover_local_media_store_scan(PLOVER_LOCAL_MEDIA_STORE(local_media));
465 show_busy_cursor(FALSE);
468 G_MODULE_EXPORT void on_help_about(GtkWidget *widget)
470 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow"));
471 gtk_show_about_dialog(GTK_WINDOW(w),"name",PACKAGE_NAME,
472 "version",PACKAGE_VERSION,"comments","Application Manager",
473 "copyright","Copyright © 2010, 2014 J. Ali Harlow",
474 "logo-icon-name",LOGO_NAME,
478 G_MODULE_EXPORT void on_find_clicked(GtkButton *button)
481 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"SearchEntry"));
482 text=g_strdup(gtk_entry_get_text(GTK_ENTRY(w)));
483 gtk_entry_set_text(GTK_ENTRY(w),"");
484 gtk_entry_set_text(GTK_ENTRY(w),text);