plover-open/plover-open.c
changeset 27 7fbec6da8123
child 30 3ee18a3a0f58
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/plover-open/plover-open.c	Mon Nov 17 11:36:20 2014 +0000
     1.3 @@ -0,0 +1,222 @@
     1.4 +/*
     1.5 + * Copyright (C) 2014  J. Ali Harlow <ali@juiblex.co.uk>
     1.6 + *
     1.7 + * This program is free software; you can redistribute it and/or modify
     1.8 + * it under the terms of the GNU General Public License as published by
     1.9 + * the Free Software Foundation; either version 2 of the License, or
    1.10 + * (at your option) any later version.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License along
    1.18 + * with this program; if not, write to the Free Software Foundation, Inc.,
    1.19 + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    1.20 + */
    1.21 +
    1.22 +#include "config.h"
    1.23 +#include <stdlib.h>
    1.24 +#include <string.h>
    1.25 +#ifdef WIN32
    1.26 +#include <windows.h>
    1.27 +#endif	/* WIN32 */
    1.28 +#include <lua.h>
    1.29 +#include <glib.h>
    1.30 +#include <gio/gio.h>
    1.31 +#include <gtk/gtk.h>
    1.32 +#include <whelk/whelk.h>
    1.33 +#include <plover/plover.h>
    1.34 +#include <plover/package.h>
    1.35 +#include <plover/packageset.h>
    1.36 +#include <plover-gtk/transactionhelper.h>
    1.37 +#include <plover-gtk/stockicons.h>
    1.38 +
    1.39 +LUALIB_API int luaopen_posix(lua_State *L);
    1.40 +
    1.41 +#define LOGO_NAME	"application-x-redhat-package-manager"
    1.42 +
    1.43 +gboolean install(PloverPackageSet *installed,const char *path)
    1.44 +{
    1.45 +    gchar *s;
    1.46 +    const char *prefix,*name,*unsatisfied;
    1.47 +    const char *filenames[2];
    1.48 +    GError *error=NULL;
    1.49 +    GSList *packages,*lnk;
    1.50 +    struct plover_vector *selected_packages;
    1.51 +    PloverPackage *package;
    1.52 +    PloverPackageSet *set;
    1.53 +    PloverRepository *upstream;
    1.54 +    PloverTransactionHelper *helper;
    1.55 +    helper=plover_transaction_helper_new(NULL);
    1.56 +    g_signal_connect(helper,"close",G_CALLBACK(gtk_main_quit),NULL);
    1.57 +    plover_transaction_helper_set_installed(helper,installed);
    1.58 +    if (plover_transaction_helper_get_error(helper,NULL))
    1.59 +    {
    1.60 +	plover_transaction_helper_present(helper);
    1.61 +	return TRUE;
    1.62 +    }
    1.63 +    filenames[0]=path;
    1.64 +    filenames[1]=NULL;
    1.65 +    upstream=plover_repository_new_from_files(filenames,&error);
    1.66 +    if (upstream)
    1.67 +	plover_transaction_helper_set_upstream(helper,upstream);
    1.68 +    else
    1.69 +    {
    1.70 +	plover_transaction_helper_set_error(helper,error,
    1.71 +	  "Software installation failed");
    1.72 +	g_clear_error(&error);
    1.73 +	plover_transaction_helper_present(helper);
    1.74 +	return TRUE;
    1.75 +    }
    1.76 +    prefix=plover_transaction_helper_get_prefix(helper,&error);
    1.77 +    if (error)
    1.78 +	g_clear_error(&error);
    1.79 +    else
    1.80 +    {
    1.81 +	s=g_strconcat(prefix?prefix:"","/var/log/open",NULL);
    1.82 +	plover_log_open(s);
    1.83 +	g_free(s);
    1.84 +    }
    1.85 +    set=plover_repository_get_package_set(upstream);
    1.86 +    packages=plover_package_set_get_packages(set);
    1.87 +    selected_packages=plover_vector_new();
    1.88 +    for(lnk=packages;lnk;lnk=lnk->next)
    1.89 +    {
    1.90 +	package=PLOVER_PACKAGE(lnk->data);
    1.91 +	plover_vector_append(selected_packages,
    1.92 +	  plover_package_get_name(package));
    1.93 +    }
    1.94 +    if (!plover_transaction_helper_install_packages(helper,selected_packages,
    1.95 +      &error))
    1.96 +    {
    1.97 +	if (g_error_matches(error,PLOVER_GENERAL_ERROR,
    1.98 +	  PLOVER_GENERAL_ERROR_NO_WORK))
    1.99 +	{
   1.100 +	    g_error_free(error);
   1.101 +	    error=g_error_new_literal(PLOVER_GENERAL_ERROR,
   1.102 +	      PLOVER_GENERAL_ERROR_NO_WORK,
   1.103 +	      "All packages already installed");
   1.104 +	    plover_transaction_helper_set_error(helper,error,
   1.105 +	      "Software installation");
   1.106 +	}
   1.107 +	else if (g_error_matches(error,PLOVER_GENERAL_ERROR,
   1.108 +	  PLOVER_GENERAL_ERROR_REQUIREMENTS_NOT_MET))
   1.109 +	{
   1.110 +	    g_error_free(error);
   1.111 +	    unsatisfied=plover_transaction_helper_get_unsatisfied(helper);
   1.112 +	    error=g_error_new(PLOVER_GENERAL_ERROR,
   1.113 +	      PLOVER_GENERAL_ERROR_REQUIREMENTS_NOT_MET,
   1.114 +	      "Software cannot be installed because the following requirements "
   1.115 +	      "were not met:\n\n%s",unsatisfied);
   1.116 +	    plover_transaction_helper_set_error(helper,error,
   1.117 +	      "Software installation failed");
   1.118 +	}
   1.119 +	else
   1.120 +	    plover_transaction_helper_set_error(helper,error,
   1.121 +	      "Software installation failed");
   1.122 +	g_error_free(error);
   1.123 +    }
   1.124 +    plover_vector_free(selected_packages);
   1.125 +    g_object_unref(upstream);
   1.126 +    plover_transaction_helper_present(helper);
   1.127 +    return TRUE;
   1.128 +}
   1.129 +
   1.130 +static void install_icons(void)
   1.131 +{
   1.132 +    GtkIconSet *icon_set;
   1.133 +    plover_icons_add_to_stock("mimetypes",LOGO_NAME);
   1.134 +    icon_set=gtk_icon_factory_lookup_default(LOGO_NAME);
   1.135 +    gtk_window_set_default_icon_name(LOGO_NAME);
   1.136 +}
   1.137 +
   1.138 +int main(int argc,char **argv)
   1.139 +{
   1.140 +    GError *err=0;
   1.141 +    GtkWidget *w;
   1.142 +    gchar *s,*t,*contents;
   1.143 +    gchar *install_path=NULL;
   1.144 +    gsize len;
   1.145 +    PloverPackageSet *set;
   1.146 +#if 0
   1.147 +    GSList *objects,*lnk;
   1.148 +#endif
   1.149 +    gboolean started;
   1.150 +    GOptionEntry options[]={
   1.151 +	{"install",0,0,G_OPTION_ARG_FILENAME,&install_path,
   1.152 +	  "Install an RPM","path"},
   1.153 +	{NULL}
   1.154 +    };
   1.155 +#ifdef WIN32
   1.156 +    /*
   1.157 +     * plover-open is normally a GUI application, but rpm scripts may well
   1.158 +     * call console applications and it looks ugly if console windows keep
   1.159 +     * popping up. Avoid this by allocating our own console and hiding it.
   1.160 +     * Note:
   1.161 +     *	- If plover-open is a console application (typically for debugging),
   1.162 +     *    then skip this step.
   1.163 +     *  - Call ShowWindow twice to negate special handling on first call.
   1.164 +     */
   1.165 +    if (!GetConsoleWindow())
   1.166 +    {
   1.167 +	AllocConsole();
   1.168 +	ShowWindow(GetConsoleWindow(),SW_HIDE);
   1.169 +	ShowWindow(GetConsoleWindow(),SW_HIDE);
   1.170 +    }
   1.171 +#endif
   1.172 +    razor_set_lua_loader("posix",luaopen_posix);
   1.173 +    razor_set_lua_loader("whelk",luaopen_whelk);
   1.174 +    if (!gtk_init_with_args(&argc,&argv,NULL,options,NULL,&err))
   1.175 +    {
   1.176 +	g_printerr("%s\n",err->message);
   1.177 +	exit(1);
   1.178 +    }
   1.179 +    install_icons();
   1.180 +#if 0
   1.181 +    ui=gtk_builder_new();
   1.182 +    if (!g_file_get_contents("plover-open.ui",&contents,&len,&err) &&
   1.183 +      g_error_matches(err,G_FILE_ERROR,G_FILE_ERROR_NOENT))
   1.184 +    {
   1.185 +#ifdef WIN32
   1.186 +	t=g_win32_get_package_installation_directory_of_module(NULL);
   1.187 +	s=g_build_filename(t,"share","plover","plover-open.ui",NULL);
   1.188 +	g_free(t);
   1.189 +#else
   1.190 +	s=g_build_filename(PLOVER_DATADIR,"plover-open.ui",NULL);
   1.191 +#endif
   1.192 +	g_clear_error(&err);
   1.193 +	(void)g_file_get_contents(s,&contents,&len,&err);
   1.194 +	g_free(s);
   1.195 +    }
   1.196 +    if (!err)
   1.197 +    {
   1.198 +	(void)gtk_builder_add_from_string(ui,contents,len,&err);
   1.199 +	g_free(contents);
   1.200 +    }
   1.201 +    if (err)
   1.202 +    {
   1.203 +	g_error("%s",err->message);
   1.204 +	exit(0);
   1.205 +    }
   1.206 +    gtk_builder_connect_signals(ui,NULL);
   1.207 +#endif
   1.208 +    set=plover_package_set_new();
   1.209 +    (void)plover_package_set_open(set,"",TRUE,NULL);
   1.210 +    if (install_path)
   1.211 +	if (install(set,install_path))
   1.212 +	    gtk_main();
   1.213 +    g_clear_object(&set);
   1.214 +#if 0
   1.215 +    objects=gtk_builder_get_objects(ui);
   1.216 +    for(lnk=objects;lnk;lnk=lnk->next)
   1.217 +	if (GTK_IS_WIDGET(lnk->data) &&
   1.218 +	  gtk_widget_is_toplevel(GTK_WIDGET(lnk->data)))
   1.219 +	    gtk_widget_destroy(GTK_WIDGET(lnk->data));
   1.220 +    g_slist_free(objects);
   1.221 +    g_clear_object(&ui);
   1.222 +#endif
   1.223 +    g_free(install_path);
   1.224 +    exit(0);
   1.225 +}