plover-open/plover-open.c
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Apr 18 15:04:30 2016 +0100 (2016-04-18)
changeset 36 f7d950c3431c
parent 30 3ee18a3a0f58
child 38 a29623b68ca2
permissions -rw-r--r--
Support newer rsvg-convert as replacement for rsvg
     1 /*
     2  * Copyright (C) 2014  J. Ali Harlow <ali@juiblex.co.uk>
     3  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 
    19 #include "config.h"
    20 #include <stdlib.h>
    21 #include <string.h>
    22 #ifdef WIN32
    23 #include <windows.h>
    24 #endif	/* WIN32 */
    25 #include <lua.h>
    26 #include <glib.h>
    27 #include <gio/gio.h>
    28 #include <gtk/gtk.h>
    29 #include <whelk/whelk.h>
    30 #include <plover/plover.h>
    31 #include <plover/package.h>
    32 #include <plover/packageset.h>
    33 #include <plover-gtk/transactionhelper.h>
    34 #include <plover-gtk/stockicons.h>
    35 
    36 LUALIB_API int luaopen_posix(lua_State *L);
    37 
    38 #define LOGO_NAME	"application-x-redhat-package-manager"
    39 
    40 gboolean install(PloverPackageSet *installed,const char *path)
    41 {
    42     gchar *s;
    43     const char *prefix,*unsatisfied;
    44     const char *filenames[2];
    45     GError *error=NULL;
    46     GSList *packages,*lnk;
    47     struct plover_vector *selected_packages;
    48     PloverPackage *package;
    49     PloverPackageSet *set;
    50     PloverRepository *upstream;
    51     PloverTransactionHelper *helper;
    52     helper=plover_transaction_helper_new(NULL);
    53     g_signal_connect(helper,"close",G_CALLBACK(gtk_main_quit),NULL);
    54     plover_transaction_helper_set_installed(helper,installed);
    55     if (plover_transaction_helper_get_error(helper,NULL))
    56     {
    57 	plover_transaction_helper_present(helper);
    58 	return TRUE;
    59     }
    60     filenames[0]=path;
    61     filenames[1]=NULL;
    62     upstream=plover_repository_new_from_files(filenames,&error);
    63     if (upstream)
    64 	plover_transaction_helper_set_upstream(helper,upstream);
    65     else
    66     {
    67 	plover_transaction_helper_set_error(helper,error,
    68 	  "Software installation failed");
    69 	g_clear_error(&error);
    70 	plover_transaction_helper_present(helper);
    71 	return TRUE;
    72     }
    73     prefix=plover_transaction_helper_get_prefix(helper,&error);
    74     if (error)
    75 	g_clear_error(&error);
    76     else
    77     {
    78 	s=g_strconcat(prefix?prefix:"","/var/log/open",NULL);
    79 	plover_log_open(s);
    80 	g_free(s);
    81     }
    82     set=plover_repository_get_package_set(upstream);
    83     packages=plover_package_set_get_packages(set);
    84     selected_packages=plover_vector_new();
    85     for(lnk=packages;lnk;lnk=lnk->next)
    86     {
    87 	package=PLOVER_PACKAGE(lnk->data);
    88 	plover_vector_append(selected_packages,
    89 	  plover_package_get_name(package));
    90     }
    91     if (!plover_transaction_helper_install_packages(helper,selected_packages,
    92       &error))
    93     {
    94 	if (g_error_matches(error,PLOVER_GENERAL_ERROR,
    95 	  PLOVER_GENERAL_ERROR_NO_WORK))
    96 	{
    97 	    g_error_free(error);
    98 	    error=g_error_new_literal(PLOVER_GENERAL_ERROR,
    99 	      PLOVER_GENERAL_ERROR_NO_WORK,
   100 	      "All packages already installed");
   101 	    plover_transaction_helper_set_error(helper,error,
   102 	      "Software installation");
   103 	}
   104 	else if (g_error_matches(error,PLOVER_GENERAL_ERROR,
   105 	  PLOVER_GENERAL_ERROR_REQUIREMENTS_NOT_MET))
   106 	{
   107 	    g_error_free(error);
   108 	    unsatisfied=plover_transaction_helper_get_unsatisfied(helper);
   109 	    error=g_error_new(PLOVER_GENERAL_ERROR,
   110 	      PLOVER_GENERAL_ERROR_REQUIREMENTS_NOT_MET,
   111 	      "Software cannot be installed because the following requirements "
   112 	      "were not met:\n\n%s",unsatisfied);
   113 	    plover_transaction_helper_set_error(helper,error,
   114 	      "Software installation failed");
   115 	}
   116 	else
   117 	    plover_transaction_helper_set_error(helper,error,
   118 	      "Software installation failed");
   119 	g_error_free(error);
   120     }
   121     plover_vector_free(selected_packages);
   122     g_object_unref(upstream);
   123     plover_transaction_helper_present(helper);
   124     return TRUE;
   125 }
   126 
   127 static void install_icons(void)
   128 {
   129     GtkIconSet *icon_set;
   130     plover_icons_add_to_stock("mimetypes",LOGO_NAME);
   131     icon_set=gtk_icon_factory_lookup_default(LOGO_NAME);
   132     gtk_window_set_default_icon_name(LOGO_NAME);
   133 }
   134 
   135 int main(int argc,char **argv)
   136 {
   137     GError *err=0;
   138     gchar *install_path=NULL;
   139     PloverPackageSet *set;
   140 #if 0
   141     gsize len;
   142     gchar *s,*t,*contents;
   143     GSList *objects,*lnk;
   144 #endif
   145     GOptionEntry options[]={
   146 	{"install",0,0,G_OPTION_ARG_FILENAME,&install_path,
   147 	  "Install an RPM","path"},
   148 	{NULL}
   149     };
   150 #ifdef WIN32
   151     /*
   152      * plover-open is normally a GUI application, but rpm scripts may well
   153      * call console applications and it looks ugly if console windows keep
   154      * popping up. Avoid this by allocating our own console and hiding it.
   155      * Note:
   156      *	- If plover-open is a console application (typically for debugging),
   157      *    then skip this step.
   158      *  - Call ShowWindow twice to negate special handling on first call.
   159      */
   160     if (!GetConsoleWindow())
   161     {
   162 	AllocConsole();
   163 	ShowWindow(GetConsoleWindow(),SW_HIDE);
   164 	ShowWindow(GetConsoleWindow(),SW_HIDE);
   165     }
   166 #endif
   167     plover_exception_handler_init();
   168     razor_set_lua_loader("posix",luaopen_posix);
   169     razor_set_lua_loader("whelk",luaopen_whelk);
   170     if (!gtk_init_with_args(&argc,&argv,NULL,options,NULL,&err))
   171     {
   172 	g_printerr("%s\n",err->message);
   173 	exit(1);
   174     }
   175     install_icons();
   176 #if 0
   177     ui=gtk_builder_new();
   178     if (!g_file_get_contents("plover-open.ui",&contents,&len,&err) &&
   179       g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
   180     {
   181 #ifdef WIN32
   182 	t=g_win32_get_package_installation_directory_of_module(NULL);
   183 	s=g_build_filename(t,"share","plover","plover-open.ui",NULL);
   184 	g_free(t);
   185 #else
   186 	s=g_build_filename(PLOVER_DATADIR,"plover-open.ui",NULL);
   187 #endif
   188 	g_clear_error(&err);
   189 	(void)g_file_get_contents(s,&contents,&len,&err);
   190 	g_free(s);
   191     }
   192     if (!err)
   193     {
   194 	(void)gtk_builder_add_from_string(ui,contents,len,&err);
   195 	g_free(contents);
   196     }
   197     if (err)
   198     {
   199 	g_error("%s",err->message);
   200 	exit(0);
   201     }
   202     gtk_builder_connect_signals(ui,NULL);
   203 #endif
   204     set=plover_package_set_new();
   205     (void)plover_package_set_open(set,"",TRUE,NULL);
   206     if (install_path)
   207 	if (install(set,install_path))
   208 	    gtk_main();
   209     g_clear_object(&set);
   210 #if 0
   211     objects=gtk_builder_get_objects(ui);
   212     for(lnk=objects;lnk;lnk=lnk->next)
   213 	if (GTK_IS_WIDGET(lnk->data) &&
   214 	  gtk_widget_is_toplevel(GTK_WIDGET(lnk->data)))
   215 	    gtk_widget_destroy(GTK_WIDGET(lnk->data));
   216     g_slist_free(objects);
   217     g_clear_object(&ui);
   218 #endif
   219     g_free(install_path);
   220     exit(0);
   221 }