Back to index

lshw  02.16
stock.c
Go to the documentation of this file.
00001 #include "stock.h"
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <gtk/gtk.h>
00005 
00006 static char *id = "@(#) $Id: stock.c 2433 2012-01-10 22:01:30Z lyonel $";
00007 
00008 #define UIFILE "gtk-lshw.ui"
00009 
00010 GtkWidget *mainwindow = NULL;
00011 GtkWidget *about = NULL;
00012 GtkWidget *list1 = NULL;
00013 GtkWidget *list2 = NULL;
00014 GtkWidget *list3 = NULL;
00015 GtkWidget *description = NULL;
00016 GtkWidget *go_up_button = NULL;
00017 GtkWidget *save_button = NULL;
00018 GtkWidget *statusbar = NULL;
00019 
00020 static struct StockIcon
00021 {
00022   const char *name;
00023   const char *filename;
00024 
00025 } const stock_icons[] =
00026 {
00027   { LSHW_STOCK_AUDIO, "audio.svg" },
00028   { LSHW_STOCK_AMD, "amd.svg" },
00029   { LSHW_STOCK_BATTERY, "battery.svg" },
00030   { LSHW_STOCK_BLUETOOTH, "bluetooth.svg" },
00031   { LSHW_STOCK_BOARD, "board.svg" },
00032   { LSHW_STOCK_CHIP, "chip.svg" },
00033   { LSHW_STOCK_CPU, "cpu.svg" },
00034   { LSHW_STOCK_DESKTOPCOMPUTER, "desktopcomputer.svg" },
00035   { LSHW_STOCK_DISABLED, "disabled.svg" },
00036   { LSHW_STOCK_DISPLAY, "display.svg" },
00037   { LSHW_STOCK_CD, "cd.svg" },
00038   { LSHW_STOCK_DISC, "disc.svg" },
00039   { LSHW_STOCK_FIREWIRE, "firewire.svg" },
00040   { LSHW_STOCK_INTEL, "intel.svg" },
00041   { LSHW_STOCK_LAPTOP, "laptop.svg" },
00042   { LSHW_STOCK_LOGO, "logo.svg" },
00043   { LSHW_STOCK_MEMORY, "memory.svg" },
00044   { LSHW_STOCK_MINI, "mini.svg" },
00045   { LSHW_STOCK_MODEM, "modem.svg" },
00046   { LSHW_STOCK_MOTHERBOARD, "motherboard.svg" },
00047   { LSHW_STOCK_NETWORK, "network.svg" },
00048   { LSHW_STOCK_PARALLEL, "parallel.svg" },
00049   { LSHW_STOCK_POWERMAC, "powermac.svg" },
00050   { LSHW_STOCK_POWERMACG5, "powermacg5.svg" },
00051   { LSHW_STOCK_POWERPC, "powerpc.svg" },
00052   { LSHW_STOCK_PRINTER, "printer.svg" },
00053   { LSHW_STOCK_RADIO, "radio.svg" },
00054   { LSHW_STOCK_MD, "md.svg" },
00055   { LSHW_STOCK_SCSI, "scsi.svg" },
00056   { LSHW_STOCK_SERIAL, "serial.svg" },
00057   { LSHW_STOCK_TOWERCOMPUTER, "towercomputer.svg" },
00058   { LSHW_STOCK_USB, "usb.svg" },
00059   { LSHW_STOCK_WIFI, "wifi.svg" },
00060 };
00061 
00062 static gchar *
00063 find_file(const char *base, const char *dir)
00064 {
00065   char *filename;
00066   char *basedir;
00067 
00068   if (base == NULL)
00069     return NULL;
00070 
00071   if((basedir = getenv("BASEDIR")))
00072     filename = g_build_filename(basedir, dir, base, NULL);
00073   else
00074     filename = g_build_filename(DATADIR, "lshw", dir, base, NULL);
00075 
00076   if (!g_file_test(filename, G_FILE_TEST_EXISTS))
00077   {
00078     g_free(filename);
00079     return NULL;
00080   }
00081   else
00082     return filename;
00083 }
00084 
00085 
00086 void
00087 lshw_gtk_stock_init(void)
00088 {
00089   static int stock_initted = 0;
00090   GtkIconFactory *icon_factory;
00091   int i;
00092   GtkWidget *win;
00093 
00094   if (stock_initted)
00095     return;
00096 
00097   stock_initted = 1;
00098 
00099 /* Setup the icon factory. */
00100   icon_factory = gtk_icon_factory_new();
00101 
00102   gtk_icon_factory_add_default(icon_factory);
00103 
00104 /* Er, yeah, a hack, but it works. :) */
00105   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00106   gtk_widget_realize(win);
00107 
00108   for (i = 0; i < G_N_ELEMENTS(stock_icons); i++)
00109   {
00110     GdkPixbuf *pixbuf;
00111     GtkIconSet *iconset;
00112     gchar *filename;
00113 
00114       filename = find_file(stock_icons[i].filename, "artwork");
00115 
00116       if (filename == NULL)
00117         continue;
00118 
00119       pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
00120       g_free(filename);
00121 
00122       if(pixbuf)     /* we managed to load something */
00123       {
00124         iconset = gtk_icon_set_new_from_pixbuf(pixbuf);
00125         g_object_unref(G_OBJECT(pixbuf));
00126         gtk_icon_factory_add(icon_factory, stock_icons[i].name, iconset);
00127         gtk_icon_set_unref(iconset);
00128       }
00129   }
00130 
00131   gtk_widget_destroy(win);
00132 
00133 /* register logo icon size */
00134   gtk_icon_size_register(LSHW_ICON_SIZE_LOGO, LSHW_DEFAULT_ICON_SIZE, LSHW_DEFAULT_ICON_SIZE);
00135 
00136   g_object_unref(G_OBJECT(icon_factory));
00137 
00138   (void) &id;                                     /* avoid "id defined but not used" warning */
00139 }
00140 
00141 void lshw_ui_init(void)
00142 {
00143   GError *error = NULL;
00144   GtkBuilder *builder = NULL;
00145   GdkPixbuf *icon = NULL;
00146   gchar *uiname = NULL;
00147 
00148   mainwindow = NULL;
00149 
00150   builder = gtk_builder_new();
00151   uiname = find_file(UIFILE, "ui");
00152   if(!uiname)
00153   {
00154     g_critical( "Could not find UI file: %s", UIFILE );
00155     return;
00156   }
00157   if(!gtk_builder_add_from_file(builder, uiname, &error))
00158   {
00159     g_critical( "Could not create UI: %s", error->message );
00160     g_free( error );
00161     g_object_unref( G_OBJECT( builder ) );
00162     return;
00163   }
00164   g_free(uiname);
00165 
00166   mainwindow = GTK_WIDGET( gtk_builder_get_object( builder, "mainwindow" ) );
00167   about = GTK_WIDGET( gtk_builder_get_object( builder, "aboutlshw" ) );
00168   list1 = GTK_WIDGET(gtk_builder_get_object( builder, "treeview1"));
00169   list2 = GTK_WIDGET(gtk_builder_get_object( builder, "treeview2"));
00170   list3 = GTK_WIDGET(gtk_builder_get_object( builder, "treeview3"));
00171   description = GTK_WIDGET(gtk_builder_get_object( builder, "description"));
00172   go_up_button = GTK_WIDGET(gtk_builder_get_object( builder, "upbutton"));
00173   save_button = GTK_WIDGET(gtk_builder_get_object( builder, "savebutton"));
00174   statusbar = GTK_WIDGET(gtk_builder_get_object( builder, "statusbar"));
00175   gtk_builder_connect_signals( builder, mainwindow );
00176   g_object_unref( G_OBJECT( builder ) );
00177 
00178   icon = gtk_widget_render_icon(GTK_WIDGET(mainwindow),
00179     "lshw-logo",
00180     GTK_ICON_SIZE_DIALOG,
00181     NULL);
00182   if(GDK_IS_PIXBUF(icon))
00183   {
00184     gtk_window_set_icon(GTK_WINDOW(mainwindow), icon);
00185     gtk_window_set_default_icon(icon);
00186   }
00187 }