diff -r 000000000000 -r e10f418d8e1d plover-open/plover-open.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plover-open/plover-open.c Sat Nov 15 19:10:51 2014 +0000 @@ -0,0 +1,222 @@ +/* + * Copyright (C) 2014 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 +#include + +LUALIB_API int luaopen_posix(lua_State *L); + +#define LOGO_NAME "application-x-redhat-package-manager" + +gboolean install(PloverPackageSet *installed,const char *path) +{ + gchar *s; + const char *prefix,*name,*unsatisfied; + const char *filenames[2]; + GError *error=NULL; + GSList *packages,*lnk; + struct plover_vector *selected_packages; + PloverPackage *package; + PloverPackageSet *set; + PloverRepository *upstream; + PloverTransactionHelper *helper; + helper=plover_transaction_helper_new(NULL); + g_signal_connect(helper,"close",G_CALLBACK(gtk_main_quit),NULL); + plover_transaction_helper_set_installed(helper,installed); + if (plover_transaction_helper_get_error(helper,NULL)) + { + plover_transaction_helper_present(helper); + return TRUE; + } + filenames[0]=path; + filenames[1]=NULL; + upstream=plover_repository_new_from_files(filenames,&error); + if (upstream) + plover_transaction_helper_set_upstream(helper,upstream); + else + { + plover_transaction_helper_set_error(helper,error, + "Software installation failed"); + g_clear_error(&error); + plover_transaction_helper_present(helper); + return TRUE; + } + prefix=plover_transaction_helper_get_prefix(helper,&error); + if (error) + g_clear_error(&error); + else + { + s=g_strconcat(prefix?prefix:"","/var/log/open",NULL); + plover_log_open(s); + g_free(s); + } + set=plover_repository_get_package_set(upstream); + packages=plover_package_set_get_packages(set); + selected_packages=plover_vector_new(); + for(lnk=packages;lnk;lnk=lnk->next) + { + package=PLOVER_PACKAGE(lnk->data); + plover_vector_append(selected_packages, + plover_package_get_name(package)); + } + if (!plover_transaction_helper_install_packages(helper,selected_packages, + &error)) + { + if (g_error_matches(error,PLOVER_GENERAL_ERROR, + PLOVER_GENERAL_ERROR_NO_WORK)) + { + g_error_free(error); + error=g_error_new_literal(PLOVER_GENERAL_ERROR, + PLOVER_GENERAL_ERROR_NO_WORK, + "All packages already installed"); + plover_transaction_helper_set_error(helper,error, + "Software installation"); + } + else if (g_error_matches(error,PLOVER_GENERAL_ERROR, + PLOVER_GENERAL_ERROR_REQUIREMENTS_NOT_MET)) + { + g_error_free(error); + unsatisfied=plover_transaction_helper_get_unsatisfied(helper); + error=g_error_new(PLOVER_GENERAL_ERROR, + PLOVER_GENERAL_ERROR_REQUIREMENTS_NOT_MET, + "Software cannot be installed because the following requirements " + "were not met:\n\n%s",unsatisfied); + plover_transaction_helper_set_error(helper,error, + "Software installation failed"); + } + else + plover_transaction_helper_set_error(helper,error, + "Software installation failed"); + g_error_free(error); + } + plover_vector_free(selected_packages); + g_object_unref(upstream); + plover_transaction_helper_present(helper); + return TRUE; +} + +static void install_icons(void) +{ + GtkIconSet *icon_set; + plover_icons_add_to_stock("mimetypes",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,*t,*contents; + gchar *install_path=NULL; + gsize len; + PloverPackageSet *set; +#if 0 + GSList *objects,*lnk; +#endif + gboolean started; + GOptionEntry options[]={ + {"install",0,0,G_OPTION_ARG_FILENAME,&install_path, + "Install an RPM","path"}, + {NULL} + }; +#ifdef WIN32 + /* + * plover-open 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 plover-open 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 + razor_set_lua_loader("posix",luaopen_posix); + razor_set_lua_loader("whelk",luaopen_whelk); + if (!gtk_init_with_args(&argc,&argv,NULL,options,NULL,&err)) + { + g_printerr("%s\n",err->message); + exit(1); + } + install_icons(); +#if 0 + ui=gtk_builder_new(); + if (!g_file_get_contents("plover-open.ui",&contents,&len,&err) && + g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT)) + { +#ifdef WIN32 + t=g_win32_get_package_installation_directory_of_module(NULL); + s=g_build_filename(t,"share","plover","plover-open.ui",NULL); + g_free(t); +#else + s=g_build_filename(PLOVER_DATADIR,"plover-open.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); +#endif + set=plover_package_set_new(); + (void)plover_package_set_open(set,"",TRUE,NULL); + if (install_path) + if (install(set,install_path)) + gtk_main(); + g_clear_object(&set); +#if 0 + 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); +#endif + g_free(install_path); + exit(0); +}