+// ===================== Implements PluginInterface =========================
+/// @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 to check which plugin gives the widget, to handle appropriately
+void MainWindow::addPluginWidget(QWidget* widget, PluginWidgetType type)
+{
+#ifdef QTRAPIDS_DEBUG
+ qDebug() << "MainWindow::addPluginWidget():" << dlView_->currentItem();
+#endif
+
+ if (type == qtrapids::PluginHostInterface::TAB_PAGE) {
+ int index = tabWidget_->addTab(widget, tr("Results"));
+ tabWidget_->setCurrentIndex(index);
+ //layout_->addWidget(widget);
+ }
+}
+void MainWindow::addToolbar(QWidget* widget, PluginWidgetType type)
+{
+}
+
+void MainWindow::addToolItem(QWidget* widget, PluginWidgetType type)
+{
+}
+
+void MainWindow::addMenu(QWidget* widget, PluginWidgetType type)
+{
+}
+
+void MainWindow::addMenuItem(QWidget* widget, PluginWidgetType type)
+{
+}
+
+bool MainWindow::eventRequest(QVariant param, PluginRequest req)
+{
+ if (req == qtrapids::PluginHostInterface::OPEN_FILE) {
+ 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;
+
+ // Copy temoporary file to Downloads directory...
+ if (!QFile::copy(sourceFile, targetFile)) {
+ qDebug() << "File copying failed";
+ return false;
+ } else {
+ // 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;
+}
+
+
+//=========================== PRIVATE ================================
+
+void MainWindow::LoadPlugins()
+{
+ // 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<qtrapids::PluginInterface*>(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 {
+ qWarning() << PLUGINS_DIR << "directory not accessible or does not exist in " << dir.path();
+ }
+ }
+}
+
+
+void MainWindow::RestoreSettings()
+{
+ btSession_.setUploadRateLimit(settings_.value("network/uploadRate").toInt());
+ btSession_.setDownloadRateLimit(settings_.value("network/downloadRate").toInt());
+}
+
+
+// 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<libtorrent::torrent_info> 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
+}
+
+// =========================== PRIVATE SLOTS =================================