app-manager/applications.c
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Jan 30 13:35:28 2012 +0000 (2012-01-30)
changeset 18 cb43820f94ce
parent 9 8d3b1ddf789c
child 24 2b9f54d14cc2
permissions -rw-r--r--
Release 0.4.1
     1 /*
     2  * Copyright (C) 2010  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
    25 #include <glib.h>
    26 #include <gtk/gtk.h>
    27 #include <plover-gtk/packagestore.h>
    28 #include "app-manager.h"
    29 
    30 #ifdef WIN32
    31 static BOOL CALLBACK plover_applications_visible_callback(HMODULE module,
    32   const char *type,char *name,long *param)
    33 {
    34     gboolean *visible=(void *)param;
    35     if (!IS_INTRESOURCE(name) && !strcmp(name,"MAINICON"))
    36 	*visible=TRUE;
    37     return !*visible;
    38 }
    39 #endif
    40 
    41 static gboolean plover_applications_visible_func(GtkTreeModel *model,
    42   GtkTreeIter *iter,gpointer data)
    43 {
    44     /* Visible if row is non-empty and package contains a .desktop file
    45      * in /usr/share/applications (UNIX) or package contains a .exe file
    46      * which has a default application icon (MS-Windows).
    47      */
    48     PloverPackage *package;
    49     GtkTreeModel *file_store;
    50     GtkTreeIter fi;
    51     gchar *name;
    52 #ifdef WIN32
    53     HMODULE module;
    54     DWORD flags=
    55 #ifdef LOAD_LIBRARY_AS_IMAGE_RESOURCE
    56       LOAD_LIBRARY_AS_IMAGE_RESOURCE|
    57 #endif
    58       LOAD_LIBRARY_AS_DATAFILE;
    59 #else
    60     gchar *dir;
    61 #endif
    62     gboolean visible=FALSE;
    63     gtk_tree_model_get(model,iter,PLOVER_PACKAGE_STORE_OBJ_COLUMN,&package,-1);
    64     if (package)
    65     {
    66 	file_store=GTK_TREE_MODEL(plover_package_get_file_store(package));
    67 	if (gtk_tree_model_get_iter_first(file_store,&fi))
    68 	{
    69 	    do
    70 	    {
    71 		gtk_tree_model_get(file_store,&fi,
    72 		  PLOVER_PACKAGE_FILE_STORE_NAME_COLUMN,&name,-1);
    73 #ifdef WIN32
    74 		if (g_str_has_suffix(name,".exe"))
    75 		{
    76 		    module=LoadLibraryExA(name,NULL,flags);
    77 		    if (module)
    78 		    {
    79 			(void)EnumResourceNamesA(module,RT_ICON,
    80 			  plover_applications_visible_callback,&visible);
    81 			if (!visible)
    82 			    (void)EnumResourceNamesA(module,RT_GROUP_ICON,
    83 			      plover_applications_visible_callback,&visible);
    84 			FreeLibrary(module);
    85 		    }
    86 		}
    87 #else
    88 		dir=g_path_get_dirname(name);
    89 		if (!strcmp(dir,"/usr/share/applications") &&
    90 		  g_str_has_suffix(name,".desktop"))
    91 		    visible=TRUE;
    92 		g_free(dir);
    93 #endif
    94 		g_free(name);
    95 	    } while(!visible && gtk_tree_model_iter_next(file_store,&fi));
    96 	}
    97     }
    98     g_object_unref(package);
    99     return visible;
   100 }
   101 
   102 GtkTreeModel *plover_applications_model_new(GtkTreeModel *installed)
   103 {
   104     GtkTreeModel *model;
   105     model=gtk_tree_model_filter_new(installed,NULL);
   106     gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(model),
   107       plover_applications_visible_func,NULL,NULL);
   108     return model;
   109 }