X-Git-Url: http://git.maemo.org/git/?p=qtrapids;a=blobdiff_plain;f=src%2Fgui%2FMainWindow.cpp;h=6ab93c6c841d1432d112d3125deab828516d602e;hp=884ced03a3b00801cb86406af5ff60a9f6b10ff0;hb=20e45e76933554432a4b4f90803be71605711377;hpb=5032546701da06d7307fc543d74be93e5319c350 diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 884ced0..6ab93c6 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -43,14 +43,18 @@ const QString ABOUT_TEXT "\nDenis Zalevskiy, denis.zalewsky@ixonos.com" "\n\nIxonos Plc, Finland\n")); +const QString PLUGINS_DIR = "plugins"; + // Consturctor MainWindow::MainWindow(): QMainWindow(), // Superclass tabWidget_(NULL), dlView_(NULL), seedView_(NULL), + searchWidget_(NULL), preferencesDialog_(NULL), settings_(), + pluginDirs_(), // torrentHandles_(), btSession_() { @@ -109,11 +113,13 @@ MainWindow::MainWindow(): SLOT(setEnabled(bool))); connect(toolBar, SIGNAL(actionTriggered(QAction*)), this, SLOT(handleToolBarAction(QAction*))); - + connect (tabWidget_, SIGNAL(tabCloseRequested(int)), this, SLOT(on_tabWidget_tabCloseRequested(int))); + connect(&btSession_, SIGNAL(alert(std::auto_ptr)), this, SLOT(on_alert(std::auto_ptr))); LoadPlugins(); + RestoreSettings(); } @@ -122,16 +128,24 @@ MainWindow::~MainWindow() } // ===================== Implements PluginInterface ========================= -bool MainWindow::setGui(QWidget* widget, PluginWidgetType type) +/// @todo add PluginInterface parameter to request plugin name +bool MainWindow::setGui(QWidget* widget, PluginWidgetType type, qtrapids::PluginInterface* plugin) { #ifdef QTRAPIDS_DEBUG qDebug() << "MainWindow::setGui():" << dlView_->currentItem(); #endif + + if (plugin && plugin->identifier() == "SearchPlugin") { + searchWidget_ = widget; + } else { + return false; + } + tabWidget_->addTab(widget, tr("Search")); return true; } -/// @todo Add PluginInterface parameter check which plugin gives the widget to handle appropriately +/// @todo Add PluginInterface parameter to check which plugin gives the widget, to handle appropriately void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type) { #ifdef QTRAPIDS_DEBUG @@ -139,7 +153,7 @@ void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type) #endif if (type == qtrapids::PluginHostInterface::TAB_PAGE) { - int index = tabWidget_->addTab(widget, tr("Test")); + int index = tabWidget_->addTab(widget, tr("Results")); tabWidget_->setCurrentIndex(index); //layout_->addWidget(widget); } @@ -163,12 +177,12 @@ void MainWindow::addMenuItem(QWidget* widget, PluginWidgetType type) bool MainWindow::eventRequest(QVariant param, PluginRequest req) { if (req == qtrapids::PluginHostInterface::OPEN_FILE) { - QString sourceFile = param.toString(); - + QString sourceFile = param.toString(); + // Get the source files name from the full path: QFileInfo fInfo(sourceFile); QString targetFile = fInfo.fileName(); - targetFile = settings_.value("download/directory").toString() + targetFile; + targetFile = settings_.value("download/directory").toString() + "/" + targetFile; // Copy temoporary file to Downloads directory... if (!QFile::copy(sourceFile, targetFile)) { @@ -178,9 +192,14 @@ bool MainWindow::eventRequest(QVariant param, PluginRequest req) // If copying was successful, remove the original temporary file. QFile::remove(sourceFile); } - + + /// @todo Torrent bencoding validity should be checked before starting(?) // ...and start the torrent: on_torrentFileSelected(targetFile); + + } else if (req == qtrapids::PluginHostInterface::READ_BUFFER) { + // Create torrent information from char* buffer and start. + StartTorrentFromBufferData(param.toByteArray().constData(), param.toByteArray().size()); } return true; @@ -191,43 +210,93 @@ bool MainWindow::eventRequest(QVariant param, PluginRequest req) void MainWindow::LoadPlugins() { - /// @todo get plugin directory from settings or go through multiple directories - /// Now we only check the application directory - pluginsDir_ = QDir(qApp->applicationDirPath()); - pluginsDir_.cd("plugins"); - QStringList nameFilters; - nameFilters << "*.so"; - - foreach (QString fileName, pluginsDir_.entryList(nameFilters, QDir::Files)) { - QPluginLoader pluginLoader(pluginsDir_.absoluteFilePath(fileName)); - - if (!QLibrary::isLibrary(fileName)) { - qDebug() << fileName << " not a library"; - } - - if (pluginLoader.load()) { - qDebug() << "Plugin loaded: " << fileName; + // Get plugin directories from + QStringList pluginDirsTmp = settings_.value("plugins/path").toStringList(); + QStringList nameFilters("*.so"); + + /// @todo enable "application directory" for plugin search in development/debug mode only. In release version + /// search plugins directory under $HOME/.qtrapids or system library paths + pluginDirsTmp << qApp->applicationDirPath(); + pluginDirsTmp.removeDuplicates(); + + foreach (QString dir, pluginDirsTmp) { + pluginDirs_.append(QDir(dir)); + } + + foreach (QDir dir, pluginDirs_) { + + if (dir.cd(PLUGINS_DIR)) { + + foreach (QString fileName, dir.entryList(nameFilters, QDir::Files)) { + QPluginLoader pluginLoader(dir.absoluteFilePath(fileName)); + + // If plugin not loaded from another directory, then load + if (!pluginFileNames_.contains(fileName) && QLibrary::isLibrary(fileName)) { + + if (pluginLoader.load()) { + qDebug() << "Plugin loaded: " << fileName; + } else { + qWarning() << "Plugin load failed: " << pluginLoader.errorString(); + } + + QObject *baseInstance = pluginLoader.instance(); + if (!baseInstance) { + qDebug() << "Base instance = NULL."; + } + + qtrapids::PluginInterface *plugin = qobject_cast(baseInstance); + + if (!plugin) { + qDebug() << "Cast failed."; + } else { + qtrapids::PluginInterface::Info info; + info.directory = dir.path(); + qDebug() << dir.path(); + plugin->initialize(this, info); + pluginFileNames_ += fileName; + } + } else { + qWarning() << "Plugin " + << fileName + << " already loaded from another directory, or not a valid library file"; + } + } + } else { - qDebug() << "Plugin load failed: " << pluginLoader.errorString(); + qWarning() << PLUGINS_DIR << "directory not accessible or does not exist in " << dir.path(); } + } +} - QObject *baseInstance = pluginLoader.instance(); - if (!baseInstance) { - qDebug() << "Base instance = NULL."; - } - qtrapids::PluginInterface *plugin = qobject_cast(baseInstance); +void MainWindow::RestoreSettings() +{ + btSession_.setUploadRateLimit(settings_.value("network/uploadRate").toInt()); + btSession_.setUploadRateLimit(settings_.value("network/downloadRate").toInt()); +} - if (!plugin) { - qDebug() << "Cast failed."; - } else { - plugin->initialize(this); - pluginFileNames_ += fileName; - } - } + +// Opens torrent information from buffer data and adds torrent to session +void MainWindow::StartTorrentFromBufferData(char const* data, int size) +{ + // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent + /// @todo Should typedef libtorrent::torrent_info to something + AddTorrentParams addParams; + boost::intrusive_ptr tiTmp = + new libtorrent::torrent_info(data, size); + addParams.ti = tiTmp; + // save_path is the only mandatory parameter, rest are optional. + addParams.save_path = boost::filesystem::path(settings_.value("download/directory").toString().toStdString()); + //addParams.storage_mode = libtorrent::storage_mode_allocate; + qtrapids::QTorrentHandle handle = btSession_.addTorrent(addParams); + dlView_->newItem(handle); +// torrentHandles_.push_back(handlePtr); +#ifdef QTRAPIDS_DEBUG + qDebug() << "Is valid: " << handle.isValid(); +#endif } -// =========================== SLOTS ================================= +// =========================== PRIVATE SLOTS ================================= void MainWindow::on_openAction_clicked() { QFileDialog *dialog = new QFileDialog( this, "Open torrent file", QString(), tr("Torrent files (*.torrent)")); @@ -242,21 +311,25 @@ void MainWindow::on_removeAction_clicked() btSession_.removeTorrent(handle); } + void MainWindow::on_quitAction_clicked() { close(); } + void MainWindow::on_preferencesAction_clicked() { if (!preferencesDialog_) { - preferencesDialog_ = new PreferencesDialog(this); + preferencesDialog_ = new PreferencesDialog(this, NULL, &btSession_); } + preferencesDialog_->show(); preferencesDialog_->raise(); preferencesDialog_->activateWindow(); } + void MainWindow::on_aboutAction_clicked() { QMessageBox::about(this, tr("About QtRapids"), ABOUT_TEXT); @@ -269,6 +342,22 @@ void MainWindow::on_aboutQtAction_clicked() } +void MainWindow::on_tabWidget_tabCloseRequested(int index) +{ + + int searchWidgetIndex = tabWidget_->indexOf(searchWidget_); + + // Allow closing other tabs than the first two + // TODO The first two may well be closable, just add "show tabs" action for these in the menu + if (index != 0 && index != 1 && index != searchWidgetIndex) { + QWidget *remove = tabWidget_->widget(index); + tabWidget_->removeTab(index); + delete remove; + remove = NULL; + } +} + + void MainWindow::on_downloadItemSelectionChanged() { #ifdef QTRAPIDS_DEBUG @@ -281,6 +370,7 @@ void MainWindow::on_downloadItemSelectionChanged() } } + void MainWindow::on_seedItemSelectionChanged() { #ifdef QTRAPIDS_DEBUG @@ -293,6 +383,7 @@ void MainWindow::on_seedItemSelectionChanged() } } + void MainWindow::handleToolBarAction(QAction* action) { if (action->text() == "Open") { @@ -302,6 +393,7 @@ void MainWindow::handleToolBarAction(QAction* action) } } + void MainWindow::on_torrentFileSelected(const QString& file) { #ifdef QTRAPIDS_DEBUG @@ -314,6 +406,7 @@ void MainWindow::on_torrentFileSelected(const QString& file) // Otherwise add torrent // For params, see: http://www.rasterbar.com/products/libtorrent/manual.html#add-torrent + /// @todo Should typedef libtorrent::torrent_info to something AddTorrentParams addParams; boost::intrusive_ptr tiTmp = new libtorrent::torrent_info(boost::filesystem::path(file.toStdString())); @@ -329,7 +422,6 @@ void MainWindow::on_torrentFileSelected(const QString& file) #endif } - void MainWindow::on_alert(std::auto_ptr al) { if (al.get() != NULL) {