/* * Copyright (C) 2010, 2020 J. Ali Harlow * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "config.h" #include #include #ifdef WIN32 #include #endif /* WIN32 */ #include #include #include #include #include #include #include #include #include "app-manager.h" #include "localmedia.h" LUALIB_API int luaopen_posix(lua_State *L); #define LOGO_NAME "plover-applications" GtkBuilder *ui; GtkTreeModel *installed,*applications,*location,*local_media; char *prefix=NULL; struct razor_relocations *relocations=NULL; void show_busy_cursor(gboolean busy) { GList *list,*link,*remaining; GdkDisplay *display; GdkCursor *cursor; GtkWidget *w; list=gtk_window_list_toplevels(); while(list) { w=GTK_WIDGET(list->data); if (!w->window) { link=list; list=g_list_remove_link(list,link); g_list_free_1(link); } else { display=gtk_widget_get_display(w); cursor=busy?gdk_cursor_new_for_display(display,GDK_WATCH):NULL; remaining=NULL; for(link=list;link;link=link->next) { w=GTK_WIDGET(link->data); if (w->window) { if (gtk_widget_get_display(w)==display) gdk_window_set_cursor(w->window,cursor); else remaining=g_list_prepend(remaining,w); } } gdk_display_flush(display); if (cursor) gdk_cursor_unref(cursor); g_list_free(list); list=remaining; } } } /* * In Gtk+ 2.16.6, the default handler generates g_warnings on error. * It should display an error to the user. Do it ourselves. */ static void show_uri(GtkLinkButton *button,const gchar *uri,gpointer data) { GdkScreen *screen; GtkWidget *dialog; GError *error=NULL; if (gtk_widget_has_screen(GTK_WIDGET(button))) screen=gtk_widget_get_screen(GTK_WIDGET(button)); else screen=NULL; gtk_show_uri(screen,uri,GDK_CURRENT_TIME,&error); if (error) { dialog=gtk_message_dialog_new( GTK_WINDOW(gtk_builder_get_object(ui,"MainWindow")), GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR,GTK_BUTTONS_CLOSE, "Unable to show '%s'",uri); gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), error->message); g_error_free(error); gtk_dialog_run(GTK_DIALOG(dialog)); gtk_widget_destroy(dialog); } } #if 0 /* Checks whether a loader for SVG files has been registered * with GdkPixbuf. */ static gboolean pixbuf_supports_svg(void) { GSList *formats; GSList *tmp_list; static gint found_svg=-1; gchar **mime_types,**mime_type; if (found_svg!=-1) return found_svg; formats=gdk_pixbuf_get_formats(); found_svg=FALSE; for (tmp_list=formats;tmp_list && !found_svg;tmp_list=tmp_list->next) { mime_types=gdk_pixbuf_format_get_mime_types(tmp_list->data); for (mime_type=mime_types;*mime_type && !found_svg;mime_type++) if (!strcmp(*mime_type,"image/svg")) found_svg=TRUE; g_strfreev(mime_types); } g_slist_free(formats); return found_svg; } static void install_icon_at_size(const char *icon_name,GtkIconSet *icon_set, GtkIconSize size,const char *filename) { int w,h; GdkPixbuf *pixbuf; GtkIconSource *source; if (gtk_icon_size_lookup(size,&w,&h)) { pixbuf=gdk_pixbuf_new_from_file_at_size(filename,w,h,NULL); if (pixbuf) { source=gtk_icon_source_new(); gtk_icon_source_set_size_wildcarded(source,FALSE); gtk_icon_source_set_size(source,size); gtk_icon_source_set_pixbuf(source,pixbuf); gtk_icon_set_add_source(icon_set,source); gtk_icon_source_free(source); g_object_unref(pixbuf); } } } static void install_icons(void) { gchar *s; GtkIconSource *source; GtkIconSet *icon_set; GtkIconFactory *factory; factory=gtk_icon_factory_new(); icon_set=gtk_icon_set_new(); if (pixbuf_supports_svg()) { source=gtk_icon_source_new(); s=g_build_filename(prefix?prefix:"/usr", "share/icons/hicolor/scalable/apps/plover-applications.svg",NULL); gtk_icon_source_set_filename(source,s); g_free(s); gtk_icon_set_add_source(icon_set,source); gtk_icon_source_free(source); } else { s=g_build_filename(prefix?prefix:"/usr", "share/icons/hicolor/24x24/apps/plover-applications.png",NULL); install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_MENU,s); install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_BUTTON,s); install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_SMALL_TOOLBAR,s); install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_LARGE_TOOLBAR,s); g_free(s); s=g_build_filename(prefix?prefix:"/usr", "share/icons/hicolor/48x48/apps/plover-applications.png",NULL); install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_DND,s); install_icon_at_size(LOGO_NAME,icon_set,GTK_ICON_SIZE_DIALOG,s); g_free(s); } gtk_icon_factory_add(factory,LOGO_NAME,icon_set); gtk_icon_set_unref(icon_set); icon_set=gtk_icon_factory_lookup(factory,LOGO_NAME); gtk_icon_factory_add_default(factory); g_object_unref(factory); icon_set=gtk_icon_factory_lookup_default(LOGO_NAME); gtk_window_set_default_icon_name(LOGO_NAME); } #endif static void install_icons(void) { GtkIconSet *icon_set; plover_icons_add_to_stock("apps",LOGO_NAME); icon_set=gtk_icon_factory_lookup_default(LOGO_NAME); gtk_window_set_default_icon_name(LOGO_NAME); } int main(int argc,char **argv) { GError *err=0; GtkWidget *w; gchar *s,*database_uri,*contents; gchar *database=NULL,*setup_base=NULL,*update_base=NULL; gchar *default_action_base=NULL; gsize len; struct comps *comps; PloverPackageSet *set=NULL; GSList *objects,*lnk; gboolean started; GOptionEntry options[]={ {"database",0,0,G_OPTION_ARG_STRING,&database, "Operate on a distribution-local database","vendor/distribution"}, {"setup",0,0,G_OPTION_ARG_STRING,&setup_base, "Setup from installation media","uri"}, {"update",0,0,G_OPTION_ARG_STRING,&update_base, "Update from upgrade media","uri"}, {"default-action",0,0,G_OPTION_ARG_STRING,&default_action_base, "Install, remove or update from repository","uri"}, {NULL} }; #ifdef WIN32 /* * app-manager is normally a GUI application, but rpm scripts may well * call console applications and it looks ugly if console windows keep * popping up. Avoid this by allocating our own console and hiding it. * Note: * - If app-manager is a console application (typically for debugging), * then skip this step. * - Call ShowWindow twice to negate special handling on first call. */ if (!GetConsoleWindow()) { AllocConsole(); ShowWindow(GetConsoleWindow(),SW_HIDE); ShowWindow(GetConsoleWindow(),SW_HIDE); } #endif plover_exception_handler_init(); razor_set_lua_loader("posix",(void (*)())luaopen_posix); razor_set_lua_loader("whelk",(void (*)())luaopen_whelk); if (!gtk_init_with_args(&argc,&argv,NULL,options,NULL,&err)) { g_printerr("%s\n",err->message); exit(1); } if (!!setup_base+!!update_base+!!default_action_base>1) { g_printerr( "--setup, --update and --default_action are mutually exclusive\n"); exit(1); } #ifdef WIN32 prefix=g_win32_get_package_installation_directory_of_module(NULL); #endif install_icons(); ui=gtk_builder_new(); if (!g_file_get_contents("app-manager.ui",&contents,&len,&err) && g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT)) { #ifdef WIN32 s=g_build_filename(prefix,"share","plover","app-manager.ui",NULL); #else s=g_build_filename(PLOVER_DATADIR,"app-manager.ui",NULL); #endif g_clear_error(&err); (void)g_file_get_contents(s,&contents,&len,&err); g_free(s); } if (!err) { (void)gtk_builder_add_from_string(ui,contents,len,&err); g_free(contents); } if (err) { g_error("%s",err->message); exit(0); } gtk_builder_connect_signals(ui,NULL); gtk_link_button_set_uri_hook(show_uri,NULL,NULL); if (setup_base) started=setup(setup_base); else if (update_base) started=update(update_base); else if (default_action_base) started=default_action(default_action_base); else { if (database) { g_free(prefix); prefix=NULL; s=strchr(database,'/'); if (*s) *s++='\0'; comps=plover_comps_new(); plover_comps_set_vendor(comps,database); if (s) { plover_comps_set_distribution(comps,s); *--s='/'; } prefix=plover_comps_get_default_prefix(comps); plover_comps_free(comps); s=g_strconcat(prefix,"/var/lib/razor",NULL); database_uri=razor_path_to_uri(s); g_free(s); razor_set_database_uri(database_uri); g_free(database_uri); } if (prefix) { relocations=razor_relocations_create(); razor_relocations_add(relocations,"/usr",prefix); } installed=GTK_TREE_MODEL(plover_package_store_new()); set=plover_package_set_new(); (void)plover_package_set_open(set,"",TRUE,NULL); plover_package_store_add_set(PLOVER_PACKAGE_STORE(installed),set); if (plover_package_set_get_no_details(set)) { w=GTK_WIDGET(gtk_builder_get_object(ui,"ViewFiles")); gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(w),TRUE); } applications=plover_applications_model_new(installed); set_package_model(applications); w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow")); gtk_widget_show(w); started=TRUE; } if (started) gtk_main(); g_clear_object(&set); objects=gtk_builder_get_objects(ui); for(lnk=objects;lnk;lnk=lnk->next) if (GTK_IS_WIDGET(lnk->data) && gtk_widget_is_toplevel(GTK_WIDGET(lnk->data))) gtk_widget_destroy(GTK_WIDGET(lnk->data)); g_slist_free(objects); g_clear_object(&ui); g_clear_object(&installed); g_clear_object(&applications); g_clear_object(&location); g_clear_object(&local_media); if (relocations) razor_relocations_destroy(relocations); g_free(prefix); g_free(setup_base); g_free(update_base); g_free(default_action_base); g_free(database); exit(0); } G_MODULE_EXPORT void on_applications_toggled(GtkToggleToolButton *button,gpointer data) { if (gtk_toggle_tool_button_get_active(button)) { if (!applications) applications=plover_applications_model_new(installed); set_package_model(applications); } } G_MODULE_EXPORT void on_all_packages_toggled(GtkToggleToolButton *button,gpointer data) { if (gtk_toggle_tool_button_get_active(button)) set_package_model(installed); } G_MODULE_EXPORT void on_local_media_toggled(GtkToggleToolButton *button,gpointer data) { if (gtk_toggle_tool_button_get_active(button)) { if (!local_media) { show_busy_cursor(TRUE); local_media=plover_local_media_store_new(); show_busy_cursor(FALSE); } set_package_model(local_media); } } G_MODULE_EXPORT void on_location_toggled(GtkToggleToolButton *button,gpointer data) { if (gtk_toggle_tool_button_get_active(button)) set_package_model(location); } G_MODULE_EXPORT void on_open_location(GtkWidget *widget) { GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow")); GtkWidget *dialog; GFile *file,*parent; GFileInfo *fi; GMount *mount; gchar *path,*name; PloverPackageSet *set; GSList *sets; GError *err=NULL; dialog=gtk_file_chooser_dialog_new("Open Location",GTK_WINDOW(w), GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,GTK_STOCK_OPEN,GTK_RESPONSE_ACCEPT,NULL); #if GTK_CHECK_VERSION(2,18,0) gtk_file_chooser_set_create_folders(GTK_FILE_CHOOSER(dialog),FALSE); #endif if (gtk_dialog_run(GTK_DIALOG(dialog))==GTK_RESPONSE_ACCEPT) { show_busy_cursor(TRUE); path=gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); set=plover_package_set_new_from_yum(path,relocations,&err); if (set) { if (!location) location=GTK_TREE_MODEL(plover_package_store_new()); while((sets= plover_package_store_get_sets(PLOVER_PACKAGE_STORE(location)))) plover_package_store_remove_set(PLOVER_PACKAGE_STORE(location), PLOVER_PACKAGE_SET(sets->data)); plover_package_store_add_set(PLOVER_PACKAGE_STORE(location),set); g_object_unref(set); w=GTK_WIDGET(gtk_builder_get_object(ui,"LocationButton")); file=g_file_new_for_path(path); parent=g_file_get_parent(file); if (parent) { g_object_unref(parent); mount=NULL; } else mount=g_file_find_enclosing_mount(file,NULL,NULL); if (mount) { name=g_mount_get_name(mount); g_object_unref(mount); } else { fi=g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME, G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,NULL,NULL); if (fi) { name=g_strdup(g_file_info_get_display_name(fi)); g_object_unref(fi); } else name=g_filename_display_basename(path); g_object_unref(file); } gtk_tool_button_set_label(GTK_TOOL_BUTTON(w),name); g_free(name); gtk_widget_show(w); gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(w),TRUE); } else { gtk_widget_destroy(dialog); dialog=gtk_message_dialog_new(GTK_WINDOW(w), GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE,"Error loading repository '%s'",path); gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s",err->message); gtk_dialog_run(GTK_DIALOG(dialog)); g_error_free(err); } g_free(path); show_busy_cursor(FALSE); } gtk_widget_destroy(dialog); } G_MODULE_EXPORT void on_scan_local_media(GtkWidget *widget) { show_busy_cursor(TRUE); if (!local_media) local_media=plover_local_media_store_new(); plover_local_media_store_scan(PLOVER_LOCAL_MEDIA_STORE(local_media)); show_busy_cursor(FALSE); } G_MODULE_EXPORT void on_help_about(GtkWidget *widget) { GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"MainWindow")); gtk_show_about_dialog(GTK_WINDOW(w),"name",PACKAGE_NAME, "version",PACKAGE_VERSION,"comments","Application Manager", "copyright","Copyright © 2010, 2014 J. Ali Harlow", "logo-icon-name",LOGO_NAME, NULL); } G_MODULE_EXPORT void on_find_clicked(GtkButton *button) { gchar *text; GtkWidget *w=GTK_WIDGET(gtk_builder_get_object(ui,"SearchEntry")); text=g_strdup(gtk_entry_get_text(GTK_ENTRY(w))); gtk_entry_set_text(GTK_ENTRY(w),""); gtk_entry_set_text(GTK_ENTRY(w),text); g_free(text); }