SearchModel.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2005-2007 by Peter Dimov.
00004 
00005 This file is part of Calitko (http://www.calitko.org).
00006 
00007 Calitko is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 2 of the License, or
00010 (at your option) any later version.
00011 
00012 Calitko is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 GNU General Public License for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Calitko; if not, write to the Free Software
00019 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021 */
00022 
00023 #ifndef SEARCHMODEL_H
00024 #define SEARCHMODEL_H
00025 
00026 #include "Gnutella/Packets/QueryHits.h"
00027 
00028 using Gnutella::Packets::QueryHits;
00029 
00030 namespace UIs {
00031 namespace Searching {
00033 
00034 struct SearchResult {
00036     SearchResult(const QString &fn, const QString &ext, const quint32 &sz, quint32 sp, QString ht, QString clt)
00037          : fileName(fn), extension(ext), speed(QString()), size (0),
00038          rating("Good"), status("Available"), host(ht),
00039          client(clt), duration("0:53:24"),
00040          bitrate("8 bps"), /*parent(NULL), */hosts(), showEmphasized (false)
00041     {
00042         float fz = sz;      // B
00043         int i = 0;
00044 
00045         while (fz > 1024) {
00046             fz /= 1024;
00047             i++;
00048         }
00049 
00050         switch (i) {
00051         case 0:
00052             size = QString().sprintf ("%.2f B", fz);
00053             break;
00054         case 1:
00055             size = QString().sprintf ("%.2f KB", fz);
00056             break;
00057         case 2:
00058             size = QString().sprintf ("%.2f MB", fz);
00059             break;
00060         default:
00061             size = QString().sprintf ("%.2f GB", fz);
00062             break;
00063         }
00064 
00065         if (sp) {
00066             speed = QString().sprintf ("%d kbps", sp);
00067         }
00068     };
00069 
00070     QString         fileName;
00071     QString         extension;
00072     QString         speed;
00073     QString         size;
00074     QString         rating;
00075     QString         status;
00076     QString         host;
00077     QString         client;
00078     QString         duration;
00079     QString         bitrate;
00080     //mutable SearchResult *parent; // Can we get rid of that?
00081     QList<SearchResult>  hosts;
00082     bool            showEmphasized;
00083 };
00084 
00085 class SearchModel : public QAbstractTableModel
00086 {
00087     Q_OBJECT
00088     REFERENCE_OBJECT (SearchModel)
00089 
00090 public:
00091     SearchModel(QObject *parent);
00092 
00093     int rowCount(const QModelIndex &parent = QModelIndex()) const;
00094     int columnCount(const QModelIndex &parent = QModelIndex()) const;
00095     QVariant data(const QModelIndex &index, int role) const;
00096     QVariant headerData(int section, Qt::Orientation orientation,
00097                         int role = Qt::DisplayRole) const;
00098     bool hasChildren (const QModelIndex &index) const;
00099     QModelIndex index(int row, int column, const QModelIndex &parent) const;
00100     //QModelIndex parent ( const QModelIndex & index ) const;
00101     void tabActivated();
00102     void tabDisactivated();
00103 
00104 private:
00105     int findIndex (SearchResult *result);
00106 
00107 private:
00108     class IconProvider
00109     {
00110     public:
00111         IconProvider();
00112         ~IconProvider() {}
00113 
00114         QIcon icon (QString fileExtension) const;
00115 
00116     private:
00117         enum FileTypes {video, audio, compressed, photo, text, unknown};
00118 
00119         QList<QIcon>    iconList;
00120         QList<QString>  videoFileExtensions;
00121         QList<QString>  audioFileExtensions;
00122         QList<QString>  compressedFileExtensions;
00123         QList<QString>  photoFileExtensions;
00124         QList<QString>  textFileExtensions;
00125     };
00126 
00127 private:
00128 //  enum SearchModelField {fileName, extension, size, rating, status, hosts, speed, client, duration, bitrate};
00129     enum SearchModelField {fileName, extension, size, hosts, speed, client};
00130 
00131     QList<SearchResult> resultList;
00132     IconProvider iconProvider;
00133     int cCount;
00134     bool tabActive;
00135 
00136 public:
00137     void addItem(SearchResult *result);
00138     void addHits(const QueryHits &quieryHits);
00139 
00140 signals:
00141     void itemAdded();
00142 
00143 };
00144 
00145 } // namespace Searching
00146 } // namespace UIs
00148 
00149 #endif  // SEARCHMODEL_H