]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/qt-console/util/fmtwidgetitem.cpp
Fix reference to uninitialized stack variable
[bacula/bacula] / bacula / src / qt-console / util / fmtwidgetitem.cpp
index f9314edf92d90d62c93839baf21abe6ff0f24e42..3971fc5eb7861d6b67afb50edc6321d3807f470d 100644 (file)
@@ -1,12 +1,12 @@
 /*
    Bacula® - The Network Backup Solution
 
-   Copyright (C) 2007-2007 Free Software Foundation Europe e.V.
+   Copyright (C) 2007-2010 Free Software Foundation Europe e.V.
 
    The main author of Bacula is Kern Sibbald, with contributions from
    many others, a complete list can be found in the file AUTHORS.
    This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version two of the GNU General Public
+   modify it under the terms of version three of the GNU Affero General Public
    License as published by the Free Software Foundation and included
    in the file LICENSE.
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    General Public License for more details.
 
-   You should have received a copy of the GNU General Public License
+   You should have received a copy of the GNU Affero General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
 
-   Bacula® is a registered trademark of John Walker.
+   Bacula® is a registered trademark of Kern Sibbald.
    The licensor of Bacula is the Free Software Foundation Europe
    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
    Switzerland, email:ftf@fsfeurope.org.
 */
  
 /*
- *   Version $Id$
  *
  *  Helper functions for tree widget formatting
  *
@@ -35,6 +34,7 @@
  *
  */ 
 
+#include "bat.h"
 #include <QTreeWidgetItem>
 #include <QTableWidget>
 #include <QTableWidgetItem>
 #include <math.h>
 #include "fmtwidgetitem.h"
 
+/***********************************************
+ *
+ * common helpers
+ *
+ ***********************************************/
+
+QString convertJobStatus(const QString &sts)
+{
+   QString code( sts.trimmed() );
+   if ( code.size() != 1) {
+      return QObject::tr("Invalid job status %1").arg(sts);
+   }
+
+   char buf[256];
+   jobstatus_to_ascii_gui( code[0].toAscii(), buf, sizeof(buf));
+   return QString(buf);
+}
+
+/*
+ * disable widget updating
+ */
+Freeze::Freeze(QWidget &q):
+qw(&q)
+{
+   qw->setUpdatesEnabled(false); 
+}
+
+Freeze::~Freeze()
+{
+   if (qw) {
+      qw->setUpdatesEnabled(true); 
+      qw->update();
+   }
+}
+
 /***********************************************
  *
  * ItemFormatterBase static members
@@ -52,7 +87,7 @@
 
 ItemFormatterBase::BYTES_CONVERSION ItemFormatterBase::cnvFlag(BYTES_CONVERSION_IEC);
 
-QString ItemFormatterBase::convertBytesIEC(qint64 qfld)
+QString convertBytesIEC(qint64 qfld)
 {
    static const qint64 KB = Q_INT64_C(1024);
    static const qint64 MB = (KB * KB);
@@ -97,7 +132,7 @@ QString ItemFormatterBase::convertBytesIEC(qint64 qfld)
    return QString("%1 %2iB").arg(qfld / 1000.0, 0, 'f', 2).arg(suffix);
 }
 
-QString ItemFormatterBase::convertBytesSI(qint64 qfld)
+QString convertBytesSI(qint64 qfld)
 {
    static const qint64 KB = Q_INT64_C(1000);
    static const qint64 MB = (KB * KB);
@@ -156,26 +191,103 @@ ItemFormatterBase::~ItemFormatterBase()
 {
 }
 
+void ItemFormatterBase::setPercent(int index, float value)
+{
+   char buf[100];
+   bsnprintf(buf, sizeof(buf), "%.2f%%", value);
+   QString val = buf;
+   QString pix;
+   if (value < 8) {
+      pix = ":images/0p.png";
+   } else if (value < 24) {
+      pix = ":images/16p.png";
+   } else if (value < 40) {
+      pix = ":images/32p.png";
+   } else if (value < 56) {
+      pix = ":images/48p.png";
+   } else if (value < 72) {
+      pix = ":images/64p.png";
+   } else if (value < 88) {
+      pix = ":images/80p.png";
+   } else {
+      pix = ":images/96p.png";
+   }
+   setPixmap(index, QPixmap(pix), val);
+   //setSortValue(index, (int) value);
+   //setBackground(index, Qt::green);
+}
+
+/* By default, the setPixmap implementation with tooltip don't implement
+ * the tooltip stuff
+ */
+void ItemFormatterBase::setPixmap(int index, const QPixmap &pix, 
+                                  const QString & /* tip */)
+{
+   setPixmap(index, pix);
+}
+
+void ItemFormatterBase::setInChanger(int index, const QString &InChanger)
+{
+   setPixmap(index, QPixmap(":images/inflag"+InChanger+".png"));
+   //setSortValue(index, InChanger.toInt() );
+}
+
+void ItemFormatterBase::setFileType(int index, const QString &type)
+{
+   setPixmap(index, QPixmap(":images/"+type+".png"));
+   //setSortValue(index, InChanger.toInt() );
+}
+
+void ItemFormatterBase::setTextFld(int index, const QString &fld, bool center)
+{
+   setText(index, fld.trimmed());
+   if (center) {
+      setTextAlignment(index, Qt::AlignCenter);
+   }
+}
+
+void ItemFormatterBase::setDateFld(int index, utime_t fld, bool center)
+{
+   char buf[200];
+   bstrutime(buf, sizeof(buf), fld);
+   setText(index, QString(buf).trimmed());
+   if (center) {
+      setTextAlignment(index, Qt::AlignCenter);
+   }
+}
+
+void ItemFormatterBase::setRightFld(int index, const QString &fld)
+{
+   setText(index, fld.trimmed());
+   setTextAlignment(index, Qt::AlignRight | Qt::AlignVCenter);
+}
+
 void ItemFormatterBase::setBoolFld(int index, const QString &fld, bool center)
 {
    if (fld.trimmed().toInt())
-     setTextFld(index, "Yes", center);
+     setTextFld(index, QObject::tr("Yes"), center);
    else
-     setTextFld(index, "No", center);
+     setTextFld(index, QObject::tr("No"), center);
 }
 
 void ItemFormatterBase::setBoolFld(int index, int fld, bool center)
 {
    if (fld)
-     setTextFld(index, "Yes", center);
+     setTextFld(index, QObject::tr("Yes"), center);
    else
-     setTextFld(index, "No", center);
+     setTextFld(index, QObject::tr("No"), center);
 }
 
 void ItemFormatterBase::setNumericFld(int index, const QString &fld)
 {
-   setTextFld(index, fld);
-   setTextAlignment(index, Qt::AlignRight | Qt::AlignVCenter);
+   setRightFld(index, fld.trimmed());
+   setSortValue(index, fld.toDouble() );
+}
+
+void ItemFormatterBase::setNumericFld(int index, const QString &fld, const QVariant &sortval)
+{
+   setRightFld(index, fld.trimmed());
+   setSortValue(index, sortval );
 }
 
 void ItemFormatterBase::setBytesFld(int index, const QString &fld)
@@ -192,8 +304,12 @@ void ItemFormatterBase::setBytesFld(int index, const QString &fld)
    case BYTES_CONVERSION_SI:
       msg = convertBytesSI(qfld);
       break;
+   default:
+      msg = " ";
+      break;
    }
-   setNumericFld(index, msg);
+
+   setNumericFld(index, msg, qfld);
 }
 
 void ItemFormatterBase::setDurationFld(int index, const QString &fld)
@@ -211,11 +327,11 @@ void ItemFormatterBase::setDurationFld(int index, const QString &fld)
    char suffix = 's';
    if (dfld) {
       for (int pos = 0 ; sufs[pos] ; ++pos) {
-         if (dfld % divs[pos] == 0) {
-            dfld /= divs[pos];
-            suffix = sufs[pos];
-            break;
-         }
+          if (dfld % divs[pos] == 0) {
+             dfld /= divs[pos];
+             suffix = sufs[pos];
+             break;
+          }
       }
    }
    QString msg;
@@ -227,69 +343,44 @@ void ItemFormatterBase::setDurationFld(int index, const QString &fld)
       dfld = fld.trimmed().toLongLong();
       msg = "";
       for (int pos = 0 ; sufs[pos] ; ++pos) {
-         if (dfld / divs[pos] != 0) {
-            msg += QString(" %1%2").arg(dfld / divs[pos]).arg(sufs[pos]);
-            dfld %= divs[pos];
-         }
+          if (dfld / divs[pos] != 0) {
+             msg += QString(" %1%2").arg(dfld / divs[pos]).arg(sufs[pos]);
+             dfld %= divs[pos];
+          }
       }
       if (dfld)
-        msg += QString(" %1s").arg(dfld);
-           
-/*
-     double net = 0;
-     QList<qlonglong> durations;
-     durations.append(1);
-     durations.append(60);
-     durations.append(HOUR);
-     durations.append(DAY);
-     durations.append(MONTH);
-     durations.append(YEAR);
-     QStringList abbrlist = (QStringList() << "s" << "min" << "h" << "d" << "m" << "y");
-     bool done = false;
-     int count = 1;
-     while (done == false) {
-       if ((dfld < durations[count]) || (count >= abbrlist.count() - 1)) { 
-           done = true;
-          net = (double)dfld / (double)(durations[count - 1]);
-          if (net != 0) {
-              msg =  QString("%1%2")
-                  .arg(net, 0, 'f', 2, QLatin1Char(' '))
-                  .arg(abbrlist[count - 1]);
-          } else {
-            msg = "0s";
-          }
-       }
-        count += 1;
-     }
-*/   }
-
-   setNumericFld(index, msg);
+         msg += QString(" %1s").arg(dfld);
+   }
+
+   setNumericFld(index, msg, fld.trimmed().toLongLong());
 }
 
 void ItemFormatterBase::setVolStatusFld(int index, const QString &fld, bool center)
 {
-   setTextFld(index, fld, center);
+  QString mp(fld.trimmed());
+   setTextFld(index, volume_status_to_str(mp.toUtf8()), center);
 
-   if (fld == "Append" ) {
+   if (mp == "Append" ) {
       setBackground(index, Qt::green);
-   } else if (fld == "Error") {
+   } else if (mp == "Error") {
       setBackground(index, Qt::red);
-   } else if (fld == "Used" || fld == "Full"){
+   } else if (mp == "Used" || mp == "Full"){
       setBackground(index, Qt::yellow);
+   } else if (mp == "Read-only" || mp == "Disabled"){
+      setBackground(index, Qt::lightGray);
    }
 }
 
-void ItemFormatterBase::setJobStatusFld(int index, const QString &shortstatus, 
-                                       const QString &longstatus, bool center)
+void ItemFormatterBase::setJobStatusFld(int index, const QString &status, bool center)
 {
    /* C (created, not yet running) uses the default background */
    static QString greenchars("TR");
    static QString redchars("BEf");
    static QString yellowchars("eDAFSMmsjdctp");
 
-   setTextFld(index, longstatus, center);
+   setTextFld(index, convertJobStatus(status), center);
 
-   QString st(shortstatus.trimmed());
+   QString st(status.trimmed());
    if (greenchars.contains(st, Qt::CaseSensitive)) {
       setBackground(index, Qt::green);
    } else if (redchars.contains(st, Qt::CaseSensitive)) {
@@ -301,29 +392,22 @@ void ItemFormatterBase::setJobStatusFld(int index, const QString &shortstatus,
 
 void ItemFormatterBase::setJobTypeFld(int index, const QString &fld, bool center)
 {
-   static QHash<QString, QString> jobt;
-   if (jobt.isEmpty()) {
-      jobt.insert("B", QObject::tr("Backup"));
-      jobt.insert("R", QObject::tr("Restore"));
-      jobt.insert("V", QObject::tr("Verify"));
-      jobt.insert("A", QObject::tr("Admin"));
+   QByteArray jtype(fld.trimmed().toAscii());
+   if (jtype.size()) {
+      setTextFld(index, job_type_to_str(jtype[0]), center);
+   } else {
+      setTextFld(index, "", center);
    }
-
-   setTextFld(index, jobt.value(fld.trimmed(), fld.trimmed()), center);
 }
 
 void ItemFormatterBase::setJobLevelFld(int index, const QString &fld, bool center)
 {
-   static QHash<QString, QString> jobt;
-   if (jobt.isEmpty()) {
-      jobt.insert("F", QObject::tr("Full"));
-      jobt.insert("D", QObject::tr("Differential"));
-      jobt.insert("I", QObject::tr("Incremental"));
-      jobt.insert("C", QObject::tr("Catalog"));
-      jobt.insert("O", QObject::tr("VolToCatalog"));
+   QByteArray lvl(fld.trimmed().toAscii());
+   if (lvl.size()) {
+      setTextFld(index, job_level_to_str(lvl[0]), center);
+   } else {
+      setTextFld(index, "", center);
    }
-
-   setTextFld(index, jobt.value(fld.trimmed(), fld.trimmed()), center);
 }
 
 
@@ -340,13 +424,10 @@ level(indent_level)
 {
 }
 
-void TreeItemFormatter::setTextFld(int index, const QString &fld, bool center)
+void TreeItemFormatter::setText(int index, const QString &fld)
 {
    wdg->setData(index, Qt::UserRole, level);
-   if (center) {
-      setTextAlignment(index, Qt::AlignCenter);
-   }
-   wdg->setText(index, fld.trimmed());
+   wdg->setText(index, fld);
 }
 
 void TreeItemFormatter::setTextAlignment(int index, int align)
@@ -359,6 +440,49 @@ void TreeItemFormatter::setBackground(int index, const QBrush &qb)
    wdg->setBackground(index, qb);
 }
 
+/* at this time we don't sort trees, so this method does nothing */
+void TreeItemFormatter::setSortValue(int /* index */, const QVariant & /* value */)
+{
+}
+
+void TreeItemFormatter::setPixmap(int index, const QPixmap &pix)
+{
+   wdg->setIcon(index, QIcon(pix));
+}
+
+/***********************************************
+ *
+ * Specialized table widget used for sorting
+ *
+ ***********************************************/
+TableItemFormatter::BatSortingTableItem::BatSortingTableItem():
+QTableWidgetItem(1)
+{
+}
+
+void TableItemFormatter::BatSortingTableItem::setSortData(const QVariant &d)
+{
+   setData(SORTDATA_ROLE, d);
+}
+
+bool TableItemFormatter::BatSortingTableItem::operator< ( const QTableWidgetItem & o ) const 
+{
+   QVariant my = data(SORTDATA_ROLE);
+   QVariant other = o.data(SORTDATA_ROLE);
+   if (!my.isValid() || !other.isValid() || my.type() != other.type())
+      return QTableWidgetItem::operator< (o); /* invalid combination, revert to default sorting */
+
+   /* 64bit integers must be handled separately, others can be converted to double */
+   if (QVariant::ULongLong == my.type()) {
+      return my.toULongLong() < other.toULongLong(); 
+   } else if (QVariant::LongLong == my.type()) {
+      return my.toLongLong() < other.toLongLong(); 
+   } else if (my.canConvert(QVariant::Double)) {
+      return my.toDouble() < other.toDouble(); 
+   } else {
+      return QTableWidgetItem::operator< (o); /* invalid combination, revert to default sorting */
+   }
+}
 
 /***********************************************
  *
@@ -373,15 +497,37 @@ last(NULL)
 {
 }
 
-void TableItemFormatter::setTextFld(int col, const QString &fld, bool center)
+void TableItemFormatter::setPixmap(int index, const QPixmap &pix)
 {
-   last = new QTableWidgetItem(1);
-/*   last->setForeground(blackBrush); */
-   parent->setItem(row, col, last);
-   if (center) {
-      setTextAlignment(col, Qt::AlignCenter);
+// Centered, but not sortable !
+   QLabel *lbl = new QLabel();
+   lbl->setAlignment(Qt::AlignCenter);
+   lbl->setPixmap(pix);
+   parent->setCellWidget(row, index, lbl);
+}
+
+void TableItemFormatter::setPixmap(int index, const QPixmap &pix, 
+                                   const QString &tips)
+{
+// Centered, but not sortable !
+   QLabel *lbl = new QLabel();
+   lbl->setAlignment(Qt::AlignCenter);
+   lbl->setPixmap(pix);
+   if (!tips.isEmpty()) {
+      lbl->setToolTip(tips);
    }
-   last->setText(fld.trimmed());
+   parent->setCellWidget(row, index, lbl);
+
+//   last = new BatSortingTableItem;
+//   parent->setItem(row, index, last);
+//   last->setIcon(pix);
+}
+
+void TableItemFormatter::setText(int col, const QString &fld)
+{
+   last = new BatSortingTableItem;
+   parent->setItem(row, col, last);
+   last->setText(fld);
 }
 
 void TableItemFormatter::setTextAlignment(int /*index*/, int align)
@@ -394,12 +540,17 @@ void TableItemFormatter::setBackground(int /*index*/, const QBrush &qb)
    last->setBackground(qb);
 }
 
+void TableItemFormatter::setSortValue(int /* index */, const QVariant &value )
+{
+   last->setSortData(value);
+}
+
 QTableWidgetItem *TableItemFormatter::widget(int col)
 {
-  return parent->item(row, col);
+   return parent->item(row, col);
 }
 
 const QTableWidgetItem *TableItemFormatter::widget(int col) const
 {
-  return parent->item(row, col);
+   return parent->item(row, col);
 }