Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2012
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.96 KB | None | 0 0
  1. ArenaWidgetFactory().create< dcpp::Singleton, FinishedDownloads >();
  2. typedef FinishedTransfers<false> FinishedDownloads;
  3.  
  4. template <bool isUpload>
  5. class FinishedTransfers :
  6.         public dcpp::FinishedManagerListener,
  7.         private Ui::UIFinishedTransfers,
  8.         public dcpp::Singleton< FinishedTransfers<isUpload> >,
  9.         public ArenaWidget,
  10.         public FinishedTransferProxy
  11. {
  12. Q_INTERFACES(ArenaWidget)
  13.  
  14. typedef QMap<QString, QVariant> VarMap;
  15. friend class dcpp::Singleton< FinishedTransfers<isUpload> >;
  16.  
  17. public:
  18.     QWidget *getWidget() { return this;}
  19.     QString getArenaTitle(){ return (isUpload? uploadTitle() : downloadTitle()); }
  20.     QString getArenaShortTitle(){ return getArenaTitle(); }
  21.     QMenu *getMenu() { return NULL; }
  22.     ArenaWidget::Role role() const;
  23.  
  24.     const QPixmap &getPixmap(){
  25.         if (isUpload)
  26.             return WICON(WulforUtil::eiUPLIST);
  27.         else
  28.             return WICON(WulforUtil::eiDOWNLIST);
  29.     }
  30.  
  31. protected:
  32.     virtual void closeEvent(QCloseEvent *e){
  33.         if (isUnload()){
  34.             QString key = (comboBox->currentIndex() == 0)? WS_FTRANSFERS_FILES_STATE : WS_FTRANSFERS_USERS_STATE;
  35.             QString state = treeView->header()->saveState().toBase64();
  36.  
  37.             WSSET(key, state);
  38.  
  39.             e->accept();
  40.         }
  41.         else {
  42.             e->ignore();
  43.         }
  44.     }
  45.  
  46. private:
  47.     FinishedTransfers(QWidget *parent = NULL) :
  48.         FinishedTransferProxy(parent), db_opened(false)
  49.     {
  50.         setupUi(this);
  51.  
  52.         model = new FinishedTransfersModel();
  53.  
  54.         proxy = new FinishedTransferProxyModel();
  55.         proxy->setDynamicSortFilter(true);
  56.         proxy->setSourceModel(model);
  57.  
  58.         treeView->setModel(proxy);
  59.  
  60. #ifdef USE_QT_SQLITE
  61.         db = QSqlDatabase::addDatabase("QSQLITE", (isUpload? "FinishedUploads" : "FinishedDownloads"));
  62.         db_file = _q(Util::getPath(Util::PATH_USER_CONFIG)) + (isUpload? "FinishedUploads.sqlite" : "FinishedDownloads.sqlite");
  63.  
  64.         db.setDatabaseName(db_file);
  65.         db_opened = db.open();
  66.  
  67.         if (db_opened){
  68.             QSqlQuery q(db);
  69.             q.exec("CREATE TABLE IF NOT EXISTS files (FNAME TEXT PRIMARY KEY, "
  70.                    "TIME TEXT, PATH TEXT, USERS TEXT, TR TEXT, SPEED TEXT, CRC32 INTEGER, TARGET TEXT, ELAP TEXT, FULL INTEGER);");
  71.  
  72.             q.exec("CREATE TABLE IF NOT EXISTS users (NICK TEXT PRIMARY KEY, "
  73.                    "TIME TEXT, FILES TEXT, TR TEXT, SPEED TEXT, CID TEXT, ELAP TEXT, FULL INTEGER);");
  74.         }
  75. #endif
  76.  
  77.         loadList();
  78.  
  79.         FinishedManager::getInstance()->addListener(this);
  80.  
  81.         setUnload(false);
  82.  
  83.         treeView->setContextMenuPolicy(Qt::CustomContextMenu);
  84.         treeView->header()->setContextMenuPolicy(Qt::CustomContextMenu);
  85.  
  86.         QObject::connect(this, SIGNAL(coreAddedFile(VarMap)),   model, SLOT(addFile(VarMap)), Qt::QueuedConnection);
  87.         QObject::connect(this, SIGNAL(coreAddedUser(VarMap)),   model, SLOT(addUser(VarMap)), Qt::QueuedConnection);
  88.         QObject::connect(this, SIGNAL(coreUpdatedFile(VarMap)), model, SLOT(addFile(VarMap)), Qt::QueuedConnection);
  89.         QObject::connect(this, SIGNAL(coreUpdatedUser(VarMap)), model, SLOT(addUser(VarMap)), Qt::QueuedConnection);
  90.         QObject::connect(this, SIGNAL(coreRemovedFile(QString)), model, SLOT(remFile(QString)), Qt::QueuedConnection);
  91.         QObject::connect(this, SIGNAL(coreRemovedUser(QString)), model, SLOT(remUser(QString)), Qt::QueuedConnection);
  92.  
  93.         QObject::connect(WulforSettings::getInstance(), SIGNAL(strValueChanged(QString,QString)), this, SLOT(slotSettingsChanged(QString,QString)));
  94.         QObject::connect(comboBox, SIGNAL(activated(int)), this, SLOT(slotTypeChanged(int)));
  95.         QObject::connect(pushButton, SIGNAL(clicked()), this, SLOT(slotClear()));
  96.         QObject::connect(treeView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotContextMenu()));
  97.         QObject::connect(treeView->header(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(slotHeaderMenu()));
  98.         QObject::connect(checkBox_FULL, SIGNAL(toggled(bool)), this, SLOT(slotSwitchOnlyFull(bool)));
  99.  
  100.         slotSwitchOnlyFull(false);
  101.         slotTypeChanged(0);
  102.        
  103.         ArenaWidget::setState( ArenaWidget::Flags(ArenaWidget::state() | ArenaWidget::Singleton | ArenaWidget::Hidden) );
  104.     }
  105.  
  106.     ~FinishedTransfers(){
  107.         FinishedManager::getInstance()->removeListener(this);
  108.  
  109.         model->clearModel();
  110.  
  111. #ifdef USE_QT_SQLITE
  112.         db.close();
  113. #endif
  114.  
  115.         delete proxy;
  116.         delete model;
  117.     }
  118.  
  119.     void loadList(){
  120.         VarMap params;
  121.  
  122.         FinishedManager::getInstance()->lockLists();
  123.         const FinishedManager::MapByFile &list = FinishedManager::getInstance()->getMapByFile(isUpload);
  124.         const FinishedManager::MapByUser &user = FinishedManager::getInstance()->getMapByUser(isUpload);
  125.  
  126.         for (FinishedManager::MapByFile::const_iterator it = list.begin(); it != list.end(); ++it){
  127.             params.clear();
  128.  
  129.             getParams(it->second, it->first, params);
  130.  
  131.             model->addFile(params);
  132.         }
  133.  
  134.         for (FinishedManager::MapByUser::const_iterator uit = user.begin(); uit != user.end(); ++uit){
  135.             params.clear();
  136.  
  137.             getParams(uit->second, uit->first, params);
  138.  
  139.             model->addUser(params);;
  140.         }
  141.  
  142.         FinishedManager::getInstance()->unlockLists();
  143.  
  144.         AsyncRunner *runner = new AsyncRunner(this);
  145.         void (*runFunc)() = loadListFromDB;
  146.         runner->setRunFunction((FinishedTransfers*)runFunc);
  147.         //runner->setRunFunction([this]() { this->loadListFromDB(); });
  148.         connect(runner, SIGNAL(finished()), runner, SLOT(deleteLater()));
  149.  
  150.         runner->start();
  151.     }
  152.  
  153.     static void loadListFromDB(){
  154. #ifdef USE_QT_SQLITE
  155.         QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", (isUpload? "FinishedUploadsLoader" : "FinishedDownloadsLoader"));
  156.         db.setDatabaseName(db_file);
  157.  
  158.         bool db_opened = db.open();
  159.  
  160.         if (!db_opened)
  161.             return;
  162.  
  163.         QSqlQuery q(db);
  164.  
  165.         q.exec("SELECT * FROM files LIMIT 0, 500;"); // temporary limitation
  166.  
  167.         VarMap params;
  168.  
  169.         while (q.next()){
  170.             int i = 0;
  171.  
  172.             params["FNAME"] = q.value(i++);
  173.             params["TIME"]  = q.value(i++);
  174.             params["PATH"]  = q.value(i++);
  175.             params["USERS"] = q.value(i++);
  176.             params["TR"]    = q.value(i++);
  177.             params["SPEED"] = q.value(i++);
  178.             params["CRC32"] = q.value(i++);
  179.             params["TARGET"]= q.value(i++);
  180.             params["ELAP"]  = q.value(i++);
  181.             params["FULL"]  = q.value(i++);
  182.  
  183.             emit coreAddedFile(params);
  184.         }
  185.  
  186.         params.clear();
  187.  
  188.         q.exec("SELECT * FROM users LIMIT 0, 500;");
  189.  
  190.         while (q.next()){
  191.             int i = 0;
  192.  
  193.             params["NICK"] = q.value(i++);
  194.             params["TIME"]  = q.value(i++);
  195.             params["FILES"]  = q.value(i++);
  196.             params["TR"]    = q.value(i++);
  197.             params["SPEED"] = q.value(i++);
  198.             params["CID"] = q.value(i++);
  199.             params["ELAP"]  = q.value(i++);
  200.             params["FULL"]  = q.value(i++);
  201.  
  202.             emit coreAddedUser(params);
  203.         }
  204.  
  205.         db.close();
  206. #endif
  207.     }
  208.  
  209.     void getParams(const FinishedFileItemPtr& item, const string& file, FinishedTransfers::VarMap &params){
  210.         QString nicks = "";
  211.  
  212.         params["FNAME"] = _q(file).split(QDir::separator()).last();
  213.         params["TIME"]  = _q(Util::formatTime("%Y-%m-%d %H:%M:%S", item->getTime()));
  214.         params["PATH"]  = _q(Util::getFilePath(file));
  215.  
  216.         for (HintedUserList::const_iterator it = item->getUsers().begin(); it != item->getUsers().end(); ++it)
  217.                 nicks += WulforUtil::getInstance()->getNicks(it->user->getCID()) + " ";
  218.  
  219.         params["USERS"] = nicks;
  220.         params["TR"]    = (qlonglong)item->getTransferred();
  221.         params["SPEED"] = (qlonglong)item->getAverageSpeed();
  222.         params["CRC32"] = item->getCrc32Checked();
  223.         params["TARGET"]= _q(file);
  224.         params["ELAP"]  = (qlonglong)item->getMilliSeconds();
  225.         params["FULL"]  = item->isFull();
  226.  
  227. #ifdef USE_QT_SQLITE
  228.         if (!db_opened)
  229.             return;
  230.  
  231.         QSqlQuery q(db);
  232.         q.prepare("REPLACE INTO files "
  233.                   "(FNAME, TIME, PATH, USERS, TR, SPEED, CRC32, TARGET, ELAP, FULL) "
  234.                   "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);");
  235.         q.bindValue(0, params["FNAME"]);
  236.         q.bindValue(1, params["TIME"]);
  237.         q.bindValue(2, params["PATH"]);
  238.         q.bindValue(3, params["USERS"]);
  239.         q.bindValue(4, params["TR"]);
  240.         q.bindValue(5, params["SPEED"]);
  241.         q.bindValue(6, params["CRC32"]);
  242.         q.bindValue(7, params["TARGET"]);
  243.         q.bindValue(8, params["ELAP"]);
  244.         q.bindValue(9, params["FULL"]);
  245.  
  246.         q.exec();
  247. #endif
  248.     }
  249.  
  250.     void getParams(const FinishedUserItemPtr& item, const UserPtr& user, FinishedTransfers::VarMap &params){
  251.         QString files = "";
  252.  
  253.         params["TIME"]  = _q(Util::formatTime("%Y-%m-%d %H:%M:%S", item->getTime()));
  254.         params["NICK"]  = WulforUtil::getInstance()->getNicks(user->getCID());
  255.  
  256.         for (StringList::const_iterator it = item->getFiles().begin(); it != item->getFiles().end(); ++it)
  257.                 files += _q(*it) + " ";
  258.  
  259.         params["FILES"] = files;
  260.         params["TR"]    = (qlonglong)item->getTransferred();
  261.         params["SPEED"] = (qlonglong)item->getAverageSpeed();
  262.         params["CID"]   = _q(user->getCID().toBase32());
  263.         params["ELAP"]  = (qlonglong)item->getMilliSeconds();
  264.         params["FULL"]  = true;
  265.  
  266. #ifdef USE_QT_SQLITE
  267.         if (!db_opened)
  268.             return;
  269.  
  270.         QSqlQuery q(db);
  271.         q.prepare("REPLACE INTO users "
  272.                   "(NICK, TIME, FILES, TR, SPEED, CID, ELAP, FULL)"
  273.                   "VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
  274.         q.bindValue(0, params["NICK"]);
  275.         q.bindValue(1, params["TIME"]);
  276.         q.bindValue(2, params["FILES"]);
  277.         q.bindValue(3, params["TR"]);
  278.         q.bindValue(4, params["SPEED"]);
  279.         q.bindValue(5, params["CID"]);
  280.         q.bindValue(6, params["ELAP"]);
  281.         q.bindValue(7, params["FULL"]);
  282.  
  283.         q.exec();
  284.  
  285. #endif
  286.     }
  287.  
  288.     void slotTypeChanged(int index){
  289.         QString from_key = (index == 0)? WS_FTRANSFERS_USERS_STATE : WS_FTRANSFERS_FILES_STATE;
  290.         QString to_key = (index == 0)? WS_FTRANSFERS_FILES_STATE : WS_FTRANSFERS_USERS_STATE;
  291.         QString old_state = treeView->header()->saveState().toBase64();
  292.  
  293.         if (sender() == comboBox)
  294.             WSSET(from_key, old_state);
  295.  
  296.         treeView->header()->restoreState(QByteArray::fromBase64(WSGET(to_key).toAscii()));
  297.         treeView->setSortingEnabled(true);
  298.  
  299.         model->switchViewType(static_cast<FinishedTransfersModel::ViewType>(index));
  300.  
  301.         if (index == FinishedTransfersModel::FileView)
  302.             proxy->setFilterKeyColumn(COLUMN_FINISHED_FULL);
  303.         else
  304.             proxy->setFilterKeyColumn(COLUMN_FINISHED_CRC32);
  305.     }
  306.  
  307.     void slotClear(){
  308.         model->clearModel();
  309.  
  310.         try {
  311.             FinishedManager::getInstance()->removeAll(isUpload);
  312.         }
  313.         catch (const std::exception&){}
  314.  
  315. #ifdef USE_QT_SQLITE
  316.         if (!db_opened)
  317.             return;
  318.  
  319.         QSqlQuery q(db);
  320.         q.exec("DROP TABLE files;");
  321.         q.exec("DROP TABLE users;");
  322. #endif
  323.     }
  324.  
  325.     void slotContextMenu(){
  326.         static WulforUtil *WU = WulforUtil::getInstance();
  327.  
  328.         QItemSelectionModel *s_model = treeView->selectionModel();
  329.         QModelIndexList p_indexes = s_model->selectedRows(0);
  330.         QModelIndexList indexes;
  331.  
  332.         foreach (const QModelIndex &i, p_indexes)
  333.             indexes.push_back(proxy->mapToSource(i));
  334.  
  335.         if (indexes.size() < 1)
  336.             return;
  337.  
  338.         QStringList files;
  339.  
  340.         if (comboBox->currentIndex() == 0){
  341.             FinishedTransfersItem *item = NULL;
  342.             QString file;
  343.  
  344.             foreach (const QModelIndex &i, indexes){
  345.                 item = reinterpret_cast<FinishedTransfersItem*>(i.internalPointer());
  346.                 file = item->data(COLUMN_FINISHED_TARGET).toString();
  347.  
  348.                 if (!file.isEmpty())
  349.                     files.push_back(file);
  350.             }
  351.         }
  352.         else {
  353.             FinishedTransfersItem *item = NULL;
  354.             QString file_list;
  355.  
  356.             foreach (const QModelIndex &i, indexes){
  357.                 item = reinterpret_cast<FinishedTransfersItem*>(i.internalPointer());
  358.                 file_list = item->data(COLUMN_FINISHED_PATH).toString();
  359.  
  360.                 if (!file_list.isEmpty()){
  361. #if QT_VERSION >= 0x040500
  362.                     files.append(file_list.split("; ", QString::SkipEmptyParts));
  363. #else
  364.                     QStringList s = file_list.split("; ", QString::SkipEmptyParts);
  365.                     foreach (const QString &i, s)
  366.                         files.push_back(i);
  367. #endif
  368.                 }
  369.  
  370.             }
  371.         }
  372.  
  373.         QMenu *m = new QMenu();
  374.         QAction *open_f   = new QAction(tr("Open file"), m);
  375.         QAction *open_dir = new QAction(WU->getPixmap(WulforUtil::eiFOLDER_BLUE), tr("Open directory"), m);
  376.  
  377.         m->addAction(open_f);
  378.         m->addAction(open_dir);
  379.  
  380.         QAction *ret = m->exec(QCursor::pos());
  381.  
  382.         delete m;
  383.  
  384.         if (ret == open_f){
  385.             foreach (QString f, files){
  386.                 if (!f.startsWith("/"))
  387.                     f.prepend("/");
  388.  
  389.                 int sep = f.lastIndexOf(QDir::separator());
  390.                 QString name = f.right(sep);
  391.                 QString path = f.left(sep);
  392.  
  393.                 QDir test(path);
  394.                 if (!test.exists(f)){
  395.                     QStringList files = test.entryList(QStringList("*"+name+"*"), QDir::Files, QDir::Name);
  396.  
  397.                     if (files.size() > 0)
  398.                         f = path + QDir::separator() + files.first();
  399.                 }
  400.  
  401.                 if (f.startsWith("/"))
  402.                     f.prepend("file://");
  403.                 else
  404.                     f.prepend("file:///");
  405.  
  406.                 QDesktopServices::openUrl(QUrl(f));
  407.             }
  408.         }
  409.         else if (ret == open_dir){
  410.             foreach (QString f, files){
  411.                 f = f.left(f.lastIndexOf(QDir::separator())) + QDir::separator();
  412.  
  413.                 if (f.startsWith("/"))
  414.                     f.prepend("file://");
  415.                 else
  416.                     f.prepend("file:///");
  417.  
  418.                 QDesktopServices::openUrl(QUrl(f));
  419.             }
  420.         }
  421.  
  422.     }
  423.  
  424.     void slotHeaderMenu(){
  425.         WulforUtil::headerMenu(treeView);
  426.     }
  427.  
  428.     void slotSwitchOnlyFull(bool checked){
  429.         proxy->setFilterFixedString((checked? "1" : ""));
  430.     }
  431.  
  432.     void slotSettingsChanged(const QString &key, const QString &){
  433.         if (key == WS_TRANSLATION_FILE)
  434.             retranslateUi(this);
  435.     }
  436.  
  437.     void on(FinishedManagerListener::AddedFile, bool upload, const std::string &file, const FinishedFileItemPtr &item) noexcept{
  438.         if (isUpload == upload){
  439.             VarMap params;
  440.  
  441.             getParams(item, file, params);
  442.  
  443.             emit coreAddedFile(params);
  444.         }
  445.     }
  446.  
  447.     void on(FinishedManagerListener::AddedUser, bool upload, const dcpp::HintedUser &user, const FinishedUserItemPtr &item) noexcept{
  448.         if (isUpload == upload){
  449.             VarMap params;
  450.  
  451.             getParams(item, user, params);
  452.  
  453.             emit coreAddedUser(params);
  454.         }
  455.     }
  456.  
  457.     void on(FinishedManagerListener::UpdatedFile, bool upload, const std::string &file, const FinishedFileItemPtr &item) noexcept{
  458.         if (isUpload == upload){
  459.             VarMap params;
  460.  
  461.             getParams(item, file, params);
  462.  
  463.             emit coreUpdatedFile(params);
  464.         }
  465.     }
  466.  
  467.     void on(FinishedManagerListener::RemovedFile, bool upload, const std::string &file) noexcept{
  468.         if (isUpload == upload){
  469.             emit coreRemovedFile(_q(file));
  470.         }
  471.     }
  472.  
  473.     void on(FinishedManagerListener::UpdatedUser, bool upload, const dcpp::HintedUser &user) noexcept{
  474.         if (isUpload == upload){
  475.             const FinishedManager::MapByUser &umap = FinishedManager::getInstance()->getMapByUser(isUpload);
  476.             FinishedManager::MapByUser::const_iterator userit = umap.find(user);
  477.             if (userit == umap.end())
  478.                 return;
  479.  
  480.             const FinishedUserItemPtr &item = userit->second;
  481.  
  482.             VarMap params;
  483.  
  484.             getParams(item, user, params);
  485.  
  486.             emit coreUpdatedUser(params);
  487.         }
  488.     }
  489.  
  490.     void on(FinishedManagerListener::RemovedUser, bool upload, const dcpp::HintedUser &user) noexcept{
  491.         if (isUpload == upload){
  492.             emit coreRemovedUser(_q(user.user->getCID().toBase32()));
  493.         }
  494.     }
  495.  
  496.     FinishedTransferProxyModel *proxy;
  497.     FinishedTransfersModel *model;
  498.  
  499. #ifdef USE_QT_SQLITE
  500.     QSqlDatabase db;
  501.     QString db_file;
  502. #endif
  503.     bool db_opened;
  504. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement