- SearchPlugin checks for Content-type -header, so now it is able to download
[qtrapids] / src / gui / MainWindow.cpp
index 9a6770c..e233aee 100644 (file)
@@ -166,12 +166,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)) {
@@ -181,9 +181,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;
@@ -252,6 +257,28 @@ void MainWindow::LoadPlugins()
        }
 }
 
+
+
+// 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
+}
+
 // =========================== SLOTS =================================
 void MainWindow::on_openAction_clicked()
 {
@@ -339,6 +366,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<libtorrent::torrent_info> tiTmp =
            new libtorrent::torrent_info(boost::filesystem::path(file.toStdString()));
@@ -354,7 +382,6 @@ void MainWindow::on_torrentFileSelected(const QString& file)
 #endif
 }
 
-
 void MainWindow::on_alert(std::auto_ptr<Alert> al)
 {
        if (al.get() != NULL) {