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/plover.h>
31 #include <plover/packageset.h>
32 #include <plover-gtk/stockicons.h>
33 #include "app-manager.h"
34 #include "localmedia.h"
36 LUALIB_API int luaopen_posix(lua_State *L);
38 #define LOGO_NAME "plover-applications"
41 GtkTreeModel *installed,*applications,*location,*local_media;
43 struct razor_relocations *relocations=NULL;
45 void show_busy_cursor(gboolean busy)
47 GList *list,*link,*remaining;
51 list=gtk_window_list_toplevels();
54 w=GTK_WIDGET(list->data);
58 list=g_list_remove_link(list,link);
63 display=gtk_widget_get_display(w);
64 cursor=busy?gdk_cursor_new_for_display(display,GDK_WATCH):NULL;
66 for(link=list;link;link=link->next)
68 w=GTK_WIDGET(link->data);
71 if (gtk_widget_get_display(w)==display)
72 gdk_window_set_cursor(w->window,cursor);
74 remaining=g_list_prepend(remaining,w);
77 gdk_display_flush(display);
79 gdk_cursor_unref(cursor);
87 * In Gtk+ 2.16.6, the default handler generates g_warnings on error.
88 * It should display an error to the user. Do it ourselves.
91 static void show_uri(GtkLinkButton *button,const gchar *uri,gpointer data)
96 if (gtk_widget_has_screen(GTK_WIDGET(button)))
97 screen=gtk_widget_get_screen(GTK_WIDGET(button));
100 gtk_show_uri(screen,uri,GDK_CURRENT_TIME,&error);
103 dialog=gtk_message_dialog_new(
104 GTK_WINDOW(gtk_builder_get_object(ui,"MainWindow")),
105 GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE,
106 "Unable to show '%s'",uri);
107 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
110 gtk_dialog_run(GTK_DIALOG(dialog));
111 gtk_widget_destroy(dialog);
116 /* Checks whether a loader for SVG files has been registered
119 static gboolean pixbuf_supports_svg(void)
123 static gint found_svg=-1;
124 gchar **mime_types,**mime_type;
127 formats=gdk_pixbuf_get_formats();
129 for (tmp_list=formats;tmp_list && !found_svg;tmp_list=tmp_list->next)
131 mime_types=gdk_pixbuf_format_get_mime_types(tmp_list->data);
132 for (mime_type=mime_types;*mime_type && !found_svg;mime_type++)
133 if (!strcmp(*mime_type,"image/svg"))
135 g_strfreev(mime_types);
137 g_slist_free(formats);
141 static void install_icon_at_size(const char *icon_name,GtkIconSet *icon_set,
142 GtkIconSize size,const char *filename)
146 GtkIconSource *source;
147 if (gtk_icon_size_lookup(size,&w,&h))
149 pixbuf=gdk_pixbuf_new_from_file_at_size(filename,w,h,NULL);
152 source=gtk_icon_source_new();
153 gtk_icon_source_set_size_wildcarded(source,FALSE);
154 gtk_icon_source_set_size(source,size);
155 gtk_icon_source_set_pixbuf(source,pixbuf);
156 gtk_icon_set_add_source(icon_set,source);
157 gtk_icon_source_free(source);
158 g_object_unref(pixbuf);
163 static void install_icons(void)
166 GtkIconSource *source;
167 GtkIconSet *icon_set;
168 GtkIconFactory *factory;
169 factory=gtk_icon_factory_new();
170 icon_set=gtk_icon_set_new();
171 if (pixbuf_supports_svg())
173 source=gtk_icon_source_new();
174 s=g_build_filename(prefix?prefix:"/usr",
175 "share/icons/hicolor/scalable/apps/plover-applications.svg",NULL);
176 gtk_icon_source_set_filename(source,s);
178 gtk_icon_set_add_source(icon_set,source);
179 gtk_icon_source_free(source);
183 s=g_build_filename(prefix?prefix:"/usr",
184 "share/icons/hicolor/24x24/apps/plover-applications.png",NULL);
185 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_MENU,s);
186 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_BUTTON,s);
187 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR,s);
188 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR,s);
190 s=g_build_filename(prefix?prefix:"/usr",
191 "share/icons/hicolor/48x48/apps/plover-applications.png",NULL);
192 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_DND,s);
193 install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_DIALOG,s);
196 gtk_icon_factory_add(factory,LOGO_NAME,icon_set);
197 gtk_icon_set_unref(icon_set);
198 icon_set=gtk_icon_factory_lookup(factory,LOGO_NAME);
199 gtk_icon_factory_add_default(factory);
200 g_object_unref(factory);
201 icon_set=gtk_icon_factory_lookup_default(LOGO_NAME);
202 gtk_window_set_default_icon_name(LOGO_NAME);
206 static void install_icons(void)
208 GtkIconSet *icon_set;
209 plover_icons_add_to_stock("apps",LOGO_NAME);
210 icon_set=gtk_icon_factory_lookup_default(LOGO_NAME);
211 gtk_window_set_default_icon_name(LOGO_NAME);
214 int main(int argc,char **argv)
219 gchar *setup_base=NULL,*update_base=NULL;
221 PloverPackageSet *set;
222 GSList *objects,*lnk;
224 GOptionEntry options[]={
225 {"setup",0,0,G_OPTION_ARG_FILENAME,&setup_base,
226 "Setup from installation media","path"},
227 {"update",0,0,G_OPTION_ARG_FILENAME,&update_base,
228 "Update from upgrade media","path"},
233 * app-manager is normally a GUI application, but rpm scripts may well
234 * call console applications and it looks ugly if console windows keep
235 * popping up. Avoid this by allocating our own console and hiding it.
237 * - If app-manager is a console application (typically for debugging),
238 * then skip this step.
239 * - Call ShowWindow twice to negate special handling on first call.
241 if (!GetConsoleWindow())
244 ShowWindow(GetConsoleWindow(),SW_HIDE);
245 ShowWindow(GetConsoleWindow(),SW_HIDE);
248 plover_exception_handler_init();
249 razor_set_lua_loader("posix",(void (*)())luaopen_posix);
250 razor_set_lua_loader("whelk",(void (*)())luaopen_whelk);
251 if (!gtk_init_with_args(&argc,&argv,NULL,options,NULL,&err))
253 g_printerr("%s\n",err->message);
256 if (setup_base && update_base)
258 g_printerr("--setup and --update are mutually exclusive\n");
262 prefix=g_win32_get_package_installation_directory_of_module(NULL);
265 ui=gtk_builder_new();
266 if (!g_file_get_contents("app-manager.ui",&contents,&len,&err) &&
267 g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
270 s=g_build_filename(prefix,"share","plover","app-manager.ui",NULL);
272 s=g_build_filename(PLOVER_DATADIR,"app-manager.ui",NULL);
275 (void)g_file_get_contents(s,&contents,&len,&err);
280 (void)gtk_builder_add_from_string(ui,contents,len,&err);
285 g_error("%s",err->message);
290 relocations=razor_relocations_create();
291 razor_relocations_add(relocations,"/usr",prefix);
293 gtk_builder_connect_signals(ui,NULL);
294 gtk_link_button_set_uri_hook(show_uri,NULL,NULL);
295 installed=GTK_TREE_MODEL(plover_package_store_new());
296 set=plover_package_set_new();
297 (void)plover_package_set_open(set,"",TRUE,NULL);
298 plover_package_store_add_set(PLOVER_PACKAGE_STORE(installed),set);
299 if (plover_package_set_get_no_details(set))
301 w=GTK_WIDGET(gtk_builder_get_object(ui,"ViewFiles"));
302 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w),TRUE);
304 applications=plover_applications_model_new(installed);
305 set_package_model(applications);
307 started=setup(set,setup_base);
308 else if (update_base)
309 started=update(set,update_base);
312 w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow"));
318 g_clear_object(&set);
319 objects=gtk_builder_get_objects(ui);
320 for(lnk=objects;lnk;lnk=lnk->next)
321 if (GTK_IS_WIDGET(lnk->data) &&
322 gtk_widget_is_toplevel(GTK_WIDGET(lnk->data)))
323 gtk_widget_destroy(GTK_WIDGET(lnk->data));
324 g_slist_free(objects);
326 g_clear_object(&installed);
327 g_clear_object(&applications);
328 g_clear_object(&location);
329 g_clear_object(&local_media);
331 razor_relocations_destroy(relocations);
339 on_applications_toggled(GtkToggleToolButton *button,gpointer data)
341 if (gtk_toggle_tool_button_get_active(button))
344 applications=plover_applications_model_new(installed);
345 set_package_model(applications);
350 on_all_packages_toggled(GtkToggleToolButton *button,gpointer data)
352 if (gtk_toggle_tool_button_get_active(button))
353 set_package_model(installed);
357 on_local_media_toggled(GtkToggleToolButton *button,gpointer data)
359 if (gtk_toggle_tool_button_get_active(button))
363 show_busy_cursor(TRUE);
364 local_media=plover_local_media_store_new();
365 show_busy_cursor(FALSE);
367 set_package_model(local_media);
372 on_location_toggled(GtkToggleToolButton *button,gpointer data)
374 if (gtk_toggle_tool_button_get_active(button))
375 set_package_model(location);
378 G_MODULE_EXPORT void on_open_location(GtkWidget *widget)
380 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow"));
386 PloverPackageSet *set;
389 dialog=gtk_file_chooser_dialog_new("Open Location",GTK_WINDOW(w),
390 GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,GTK_STOCK_CANCEL,
391 GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL);
392 #if GTK_CHECK_VERSION(2,18,0)
393 gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog),FALSE);
395 if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_ACCEPT)
397 show_busy_cursor(TRUE);
398 path=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
399 set=plover_package_set_new_from_yum(path,relocations,&err);
403 location=GTK_TREE_MODEL(plover_package_store_new());
405 plover_package_store_get_sets(PLOVER_PACKAGE_STORE(location))))
406 plover_package_store_remove_set(PLOVER_PACKAGE_STORE(location),
407 PLOVER_PACKAGE_SET(sets->data));
408 plover_package_store_add_set(PLOVER_PACKAGE_STORE(location),set);
410 w=GTK_WIDGET(gtk_builder_get_object(ui,"LocationButton"));
411 file=g_file_new_for_path(path);
412 parent=g_file_get_parent(file);
415 g_object_unref(parent);
419 mount=g_file_find_enclosing_mount(file,NULL,NULL);
422 name=g_mount_get_name(mount);
423 g_object_unref(mount);
427 fi=g_file_query_info(file,
428 G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME,
429 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,NULL,NULL);
432 name=g_strdup(g_file_info_get_display_name(fi));
436 name=g_filename_display_basename(path);
437 g_object_unref(file);
439 gtk_tool_button_set_label(GTK_TOOL_BUTTON(w),name);
442 gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(w),TRUE);
446 gtk_widget_destroy(dialog);
447 dialog=gtk_message_dialog_new(GTK_WINDOW(w),
448 GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR,
449 GTK_BUTTONS_CLOSE,"Error loading repository '%s'",path);
450 gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog),
452 gtk_dialog_run(GTK_DIALOG(dialog));
456 show_busy_cursor(FALSE);
458 gtk_widget_destroy(dialog);
461 G_MODULE_EXPORT void on_scan_local_media(GtkWidget *widget)
463 show_busy_cursor(TRUE);
465 local_media=plover_local_media_store_new();
466 plover_local_media_store_scan(PLOVER_LOCAL_MEDIA_STORE(local_media));
467 show_busy_cursor(FALSE);
470 G_MODULE_EXPORT void on_help_about(GtkWidget *widget)
472 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow"));
473 gtk_show_about_dialog(GTK_WINDOW(w),"name",PACKAGE_NAME,
474 "version",PACKAGE_VERSION,"comments","Application Manager",
475 "copyright","Copyright © 2010, 2014 J. Ali Harlow",
476 "logo-icon-name",LOGO_NAME,
480 G_MODULE_EXPORT void on_find_clicked(GtkButton *button)
483 GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"SearchEntry"));
484 text=g_strdup(gtk_entry_get_text(GTK_ENTRY(w)));
485 gtk_entry_set_text(GTK_ENTRY(w),"");
486 gtk_entry_set_text(GTK_ENTRY(w),text);