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