Applets
hwinfo.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "hwinfo.h"
00020 #include <Plasma/WebView>
00021 #include <Plasma/IconWidget>
00022 #include <Plasma/Containment>
00023 #include <Plasma/ToolTipManager>
00024 #include <Plasma/Theme>
00025 #include <KStandardDirs>
00026 #include <KIcon>
00027 #include <KTextEdit>
00028 #include <QTextDocument>
00029 #include <QGraphicsLinearLayout>
00030
00031 #define START "<html><head><style type=\"text/css\">\
00032 body { background-color: %1; } \
00033 td { vertical-align: top; font-size:7pt; font-weight:normal; font-style:normal; color: %2; } \
00034 </style></head><body>"
00035 #define START_BASIC "<html><head></head><body>"
00036 #define START_TABLE "<table>"
00037 #define INFO_ROW "<tr><td>%1:</td><td>%2</td></tr>"
00038 #define END_TABLE "</table>"
00039 #define END "</body><html>"
00040
00041 HWInfo::HWInfo(QObject *parent, const QVariantList &args)
00042 : SM::Applet(parent, args), m_info(0), m_icon(0)
00043 {
00044 setHasConfigurationInterface(false);
00045 resize(234 + 20 + 23, 135 + 20 + 25);
00046 setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
00047 connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(updateHtml()));
00048 }
00049
00050 HWInfo::~HWInfo()
00051 {
00052 }
00053
00054 void HWInfo::init()
00055 {
00056 setTitle(i18n("Hardware Info"));
00057 appendItem("info");
00058 connectToEngine();
00059 }
00060
00061 bool HWInfo::addMeter(const QString&)
00062 {
00063 if (mode() != SM::Applet::Panel) {
00064 m_info = new Plasma::WebView(this);
00065 m_info->setHtml(QString(START + i18n("Getting hardware information...") + END));
00066 m_icon = 0;
00067 mainLayout()->addItem(m_info);
00068
00069
00070 setPreferredItemHeight(135);
00071 } else {
00072 m_icon = new Plasma::IconWidget(KIcon("hwinfo"), QString(), this);
00073 m_info = 0;
00074 mainLayout()->addItem(m_icon);
00075 }
00076 return false;
00077 }
00078
00079 void HWInfo::connectToEngine()
00080 {
00081 Applet::connectToEngine();
00082 setEngine(dataEngine("soliddevice"));
00083
00084 m_cpus = engine()->query("IS Processor")["IS Processor"].toStringList();
00085 foreach (const QString& id, m_cpus) {
00086 engine()->connectSource(id, this);
00087 }
00088 m_networks = engine()->query("IS NetworkInterface")["IS NetworkInterface"].toStringList();
00089 foreach (const QString& id, m_networks) {
00090 engine()->connectSource(id, this);
00091 }
00092 m_audios = engine()->query("IS AudioInterface")["IS AudioInterface"].toStringList();
00093 foreach (const QString& id, m_audios) {
00094 engine()->connectSource(id, this);
00095 }
00096
00097 Plasma::DataEngine* engine = dataEngine("executable");
00098 QString path = QString::fromLocal8Bit(getenv("PATH")) + QString::fromLatin1(":/usr/sbin:/sbin/");
00099 QString exe = KStandardDirs::findExe( "lspci", path );
00100 if (exe.isEmpty())
00101 kError() << "lspci not found in " << path << endl;
00102 else
00103 {
00104 QString tmp = exe + " | grep VGA | sed 's/.*: //g'";
00105 engine->connectSource(tmp, this);
00106 }
00107 }
00108
00109 void HWInfo::dataUpdated(const QString& source,
00110 const Plasma::DataEngine::Data &data)
00111 {
00112 if (m_audios.contains(source) && !m_audioNames.contains(data["Name"].toString()) &&
00113 !data["Name"].toString().isEmpty()) {
00114 m_audioNames.append(data["Name"].toString());
00115 } else if (m_networks.contains(source) && !m_networkNames.contains(data["Product"].toString()) &&
00116 !data["Product"].toString().isEmpty()) {
00117 m_networkNames.append(data["Product"].toString());
00118 } else if (m_cpus.contains(source) && !m_cpuNames.contains(data["Product"].toString()) &&
00119 !data["Product"].toString().isEmpty()) {
00120 m_cpuNames.append(data["Product"].toString().trimmed());
00121 } else if (source.indexOf("VGA") > -1) {
00122 m_gpu = data["stdout"].toString().trimmed();
00123 }
00124 updateHtml();
00125 }
00126
00127 void HWInfo::updateHtml()
00128 {
00129 QString html;
00130 foreach(const QString& cpu, m_cpuNames) {
00131 html += QString(INFO_ROW).arg(i18n("CPU")).arg(cpu);
00132 }
00133 html += QString(INFO_ROW).arg(i18n("GPU")).arg(m_gpu);
00134 foreach(const QString& audio, m_audioNames) {
00135 html += QString(INFO_ROW).arg(i18n("Audio")).arg(audio);
00136 }
00137 foreach(const QString& network, m_networkNames) {
00138 html += QString(INFO_ROW).arg(i18n("Network")).arg(network);
00139 }
00140 html += END_TABLE END;
00141 if (m_info) {
00142 Plasma::Theme* theme = Plasma::Theme::defaultTheme();
00143 html = QString(START START_TABLE)
00144 .arg(theme->color(Plasma::Theme::BackgroundColor).name())
00145 .arg(theme->color(Plasma::Theme::TextColor).name()) + html;
00146 m_info->setHtml(html);
00147 } else if (m_icon) {
00148 html = START_BASIC START_TABLE + html;
00149 Plasma::ToolTipContent data(i18n("Hardware Info"), html);
00150 Plasma::ToolTipManager::self()->setContent(m_icon, data);
00151 }
00152 }
00153
00154 #include "hwinfo.moc"