Back to index

lshw  02.16
print-gui.cc
Go to the documentation of this file.
00001 /*
00002  * print.cc
00003  *
00004  *
00005  */
00006 
00007 #include "print-gui.h"
00008 #include "options.h"
00009 #include "config.h"
00010 #include "version.h"
00011 #include "osutils.h"
00012 #include "stock.h"
00013 #include <sstream>
00014 #include <iomanip>
00015 #include <unistd.h>
00016 #include <stdio.h>
00017 
00018 static const char *id = "@(#) $Id: print-gui.cc 2433 2012-01-10 22:01:30Z lyonel $";
00019 
00020 static void printattr(const string & name, const string & value, GtkTextBuffer *buffer, GtkTextIter &iter)
00021 {
00022   if(value == "") return;
00023 
00024   gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, name.c_str(), -1, "bold", NULL);
00025   gtk_text_buffer_insert (buffer, &iter, ": ", -1);
00026   gtk_text_buffer_insert (buffer, &iter, value.c_str(), -1);
00027   gtk_text_buffer_insert (buffer, &iter, "\n", -1);
00028 }
00029 
00030 
00031 static void printsize(long long value, const hwNode & node, const string & name, GtkTextBuffer *buffer, GtkTextIter &iter)
00032 {
00033   if (value > 0)
00034   {
00035     switch (node.getClass())
00036     {
00037       case hw::disk:
00038         printattr(name,kilobytes(value)+" ("+decimalkilos(value)+"B)", buffer, iter);
00039         break;
00040       case hw::display:
00041       case hw::memory:
00042       case hw::address:
00043       case hw::storage:
00044       case hw::volume:
00045         printattr(name,kilobytes(value), buffer, iter);
00046         break;
00047 
00048       case hw::processor:
00049       case hw::bus:
00050       case hw::system:
00051         printattr(name,decimalkilos(value)+"Hz", buffer, iter);
00052         break;
00053 
00054       case hw::network:
00055         printattr(name,decimalkilos(value)+"bit/s", buffer, iter);
00056         break;
00057 
00058       case hw::power:
00059         printattr(name,tostring(value)+"mWh", buffer, iter);
00060         break;
00061 
00062       default:
00063         printattr(name,tostring(value), buffer, iter);
00064     }
00065   }
00066 }
00067 
00068 
00069 static  void inserticon(const string & icon, const string & comment, GtkTextBuffer *buffer, GtkTextIter &iter, GtkTextView * textview)
00070 {
00071   GdkPixbuf *pixbuf;
00072   GtkTextTag *tag;
00073 
00074   pixbuf = gtk_widget_render_icon(GTK_WIDGET(textview),
00075     icon.c_str(),
00076     gtk_icon_size_from_name(LSHW_ICON_SIZE_LOGO), /* size */
00077     NULL);
00078   if(!GDK_IS_PIXBUF(pixbuf))
00079     return;
00080 
00081   tag = gtk_text_buffer_create_tag (buffer, NULL, 
00082                                 "rise", 2*LSHW_DEFAULT_ICON_SIZE*PANGO_SCALE/5,
00083                                 NULL);
00084   gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
00085   gtk_text_buffer_insert_with_tags(buffer, &iter, comment.c_str(), -1, tag, NULL);
00086 //gtk_text_buffer_insert(buffer, &iter, "\n", -1);
00087 }
00088 
00089 
00090 void printmarkup(const hwNode & node, GtkTextView *textview, const string & hwpath)
00091 {
00092   vector < string > config;
00093   vector < string > resources;
00094   ostringstream out;
00095   GtkTextIter iter;
00096   GtkTextBuffer *buffer;
00097 
00098   buffer = gtk_text_view_get_buffer(textview);
00099 
00100   gtk_text_buffer_get_iter_at_offset (buffer, &iter, 0);
00101 
00102   resources = node.getResources(":");
00103 
00104   if(node.getDescription()!="")
00105     gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, node.getDescription().c_str(), -1, "heading", NULL);
00106   else
00107   {
00108     if(node.getProduct()!="")
00109       gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, node.getProduct().c_str(), -1, "heading", NULL);
00110     else
00111       gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, node.getId().c_str(), -1, "heading", NULL);
00112   }
00113   if(!node.claimed() || node.disabled())
00114     out << "<span color=\"gray\">";
00115   if(node.disabled())
00116     out << _(" DISABLED");
00117   if(!node.claimed())
00118     out << _(" UNCLAIMED");
00119   if(!node.claimed() || node.disabled())
00120     out << "</span>";
00121 
00122   gtk_text_buffer_insert (buffer, &iter, "\n", -1);
00123   gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, hwpath.c_str(), -1, "monospace", NULL);
00124 
00125   gtk_text_buffer_insert (buffer, &iter, "\n", -1);
00126 
00127   if(node.getHint("icon").defined())
00128     inserticon(string("lshw-") + node.getHint("icon").asString(), "", buffer, iter, textview);
00129 
00130   if(node.getHint("bus.icon").defined())
00131     inserticon(string("lshw-") + node.getHint("bus.icon").asString(), "", buffer, iter, textview);
00132 
00133   if(node.getHint("logo").defined())
00134     inserticon(string("lshw-") + node.getHint("logo").asString(), "", buffer, iter, textview);
00135 
00136   gtk_text_buffer_insert (buffer, &iter, "\n\n", -1);
00137 
00138 //out << printattr("description", node.getDescription());
00139   printattr(_("product"), node.getProduct(), buffer, iter);
00140   printattr(_("vendor"), node.getVendor(), buffer, iter);
00141   printattr(_("bus info"), node.getBusInfo(), buffer, iter);
00142   if (node.getLogicalName() != "")
00143   {
00144     vector<string> logicalnames = node.getLogicalNames();
00145 
00146     for(unsigned int i = 0; i<logicalnames.size(); i++)
00147       printattr(_("logical name"), logicalnames[i], buffer, iter);
00148   }
00149 
00150   printattr(_("version"), node.getVersion(), buffer, iter);
00151   printattr(_("serial"), node.getSerial(), buffer, iter);
00152   printattr(_("slot"), node.getSlot(), buffer, iter);
00153 
00154   if(node.getSize() > 0)
00155     printsize(node.getSize(), node, _("size"), buffer, iter);
00156   if(node.getCapacity() > 0)
00157     printsize(node.getCapacity(), node, _("capacity"), buffer, iter);
00158 
00159   if(node.getWidth() > 0)
00160     printattr(_("width"),tostring(node.getWidth())+" bits", buffer, iter);
00161 
00162   if(node.getClock() > 0)
00163     printattr(_("clock"), decimalkilos(node.getClock())+string("Hz") + ((node.getClass() == hw::memory)?(string(" (")+tostring((long long)(1.0e9 / node.getClock())) + string("ns)")):string("")), buffer, iter);
00164 
00165   config.clear();
00166   splitlines(node.getCapabilities(), config, ' ');
00167 
00168   if (config.size() > 0)
00169   {
00170     gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, _("capabilities"), -1, "bold", NULL);
00171     gtk_text_buffer_insert (buffer, &iter, ":", -1);
00172     for(unsigned j=0; j<config.size(); j++)
00173     {
00174       gtk_text_buffer_insert (buffer, &iter, "\n\t", -1);
00175       if(node.getCapabilityDescription(config[j]) != "")
00176       {
00177         gtk_text_buffer_insert (buffer, &iter, node.getCapabilityDescription(config[j]).c_str(), -1);
00178       }
00179       else
00180         gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, config[j].c_str(), -1, "italic", NULL);
00181       if(j<config.size()-1)
00182         gtk_text_buffer_insert (buffer, &iter, ",", -1);
00183     }
00184   }
00185 
00186   gtk_text_buffer_insert (buffer, &iter, "\n", -1);
00187 
00188   config.clear();
00189   config = node.getConfigValues(": ");
00190 
00191   if (config.size() > 0)
00192   {
00193     gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, _("configuration"), -1, "bold", NULL);
00194     gtk_text_buffer_insert (buffer, &iter, ":", -1);
00195     for(unsigned j=0; j<config.size(); j++)
00196     {
00197       gtk_text_buffer_insert (buffer, &iter, "\n\t", -1);
00198       gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, config[j].c_str(), -1, "italic", NULL);
00199     }
00200   }
00201 
00202   gtk_text_buffer_insert (buffer, &iter, "\n", -1);
00203 
00204   config.clear();
00205   config = node.getResources(": ");
00206 
00207   if (config.size() > 0)
00208   {
00209     gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, _("resources"), -1, "bold", NULL);
00210     gtk_text_buffer_insert (buffer, &iter, ":", -1);
00211     for(unsigned j=0; j<config.size(); j++)
00212     {
00213       gtk_text_buffer_insert (buffer, &iter, "\n\t", -1);
00214       gtk_text_buffer_insert_with_tags_by_name (buffer, &iter, config[j].c_str(), -1, "italic", NULL);
00215     }
00216   }
00217 
00218   gtk_text_buffer_insert (buffer, &iter, "\n", -1);
00219 
00220   if(!node.claimed())
00221     inserticon(LSHW_STOCK_DISABLED, _("this device hasn't been claimed\n"), buffer, iter, textview);
00222 
00223   if(!node.enabled())
00224     inserticon(LSHW_STOCK_DISABLED, _("this device has been disabled\n"), buffer, iter, textview);
00225 
00226   (void) &id;                                     // avoid "id defined but not used" warning
00227 }
00228 
00229 
00230 static hwNode * find_parent(hwNode * n, hwNode *sub)
00231 {
00232   if(!n) return NULL;
00233 
00234   if(n == sub) return n;
00235 
00236   for(unsigned i=0; i<sub->countChildren(); i++)
00237   {
00238     if(sub->getChild(i) == n) return sub;
00239 
00240     hwNode *r = find_parent(n, sub->getChild(i));
00241     if(r) return r;
00242   }
00243 
00244   return NULL;
00245 }
00246 
00247 
00248 string gethwpath( hwNode & n, hwNode & base)
00249 {
00250   ostringstream out;
00251   hwNode *parent = find_parent(&n, &base);
00252 
00253   if(parent && (parent != &base))
00254     out << gethwpath(*parent, base) << "/" << n.getPhysId();
00255 
00256   return out.str();
00257 }