File magnet-mod.diff of Package valknut

diff -Naur valknut-0.3.12/valknut/main.cpp valknut-0.3.12-mod/valknut/main.cpp
--- valknut-0.3.23.orig/valknut/main.cpp	2009-01-17 21:00:56.000000000 +0300
+++ valknut-0.3.23/valknut/main.cpp	2013-11-27 09:19:14.346838388 +0400
@@ -35,10 +35,12 @@
 #include <qlabel.h>
 #include <qstylefactory.h>
 #include <private/qinternal_p.h>
+#include <qfile.h>
 
 #include "dcconfig.h"
 #include "dcsplash.h"
 #include "dcgui.h"
+#include "dcwaitmagnet.h"
 #include "dcpluginmanager.h"
 #include "dcconnectionmanager.h"
 #include "dctransferview.h"
@@ -151,6 +153,7 @@
 	int ret;
 	CString configpath;
 	CString debugopt;
+	CString magnet = "";
 	bool bcrash = false;
 	QTranslator tor(0);
 	DCSplash * pSplash = 0;
@@ -208,6 +211,7 @@
 			s += "-c <configpath>     - set another config path\n";
 			s += "-v                  - verbose \n";
 			s += "-vv                 - more verbose \n";
+			s += "-m <magnet>         - auto search file by magnet-link\n";
 			s += "--trayicon          - start in tray icon mode\n";
 			s += "--socketlog <param> - log socket to dcsocket.log file (param = [none,send,recv,both])\n";
 			s += "\n";
@@ -258,6 +262,19 @@
 				i--;
 			}
 		}
+		else if ( QString(argv[i]) == "-m" )
+		{
+			i++;
+			if ( i<argc )
+			{
+				magnet = argv[i];
+			}
+			else
+			{
+				printf("Wrong parameter option -m\n");
+				i--;
+			}
+		}
 		else if ( QString(argv[i]) == "--trayicon" )
 		{
 			bTray = true;
@@ -352,6 +369,54 @@
 
 	CConfig::SetInstance(new DCConfig(configpath));
 
+	// Extract tth from magnet-link
+	if(magnet.Length() > 0) {
+		CString tth = "";
+		int i = magnet.Find("xt=urn:tree:tiger:");
+		if (i == -1)
+		{
+			i = magnet.Find("xt.1=urn:tree:tiger:");
+			tth = magnet.Mid(i + 20, 39).Data();
+		}
+		else
+		{
+			tth = magnet.Mid(i + 18, 39).Data();
+		}
+		if(tth.Length() == 39)
+		{
+			QFile *file = new QFile((QString)g_pConfig->GetConfigPath().Data() + DIRSEPARATOR + "magnet");
+			file->open(IO_WriteOnly);
+			char *block = tth.Data();
+			printf("Found TTH: %s\n", block);
+			file->writeBlock(block, strlen(block));
+			file->close();
+			delete file;
+		} else
+			printf("Magnet-link is bad!\n");
+	}
+
+	// Exit if process already exists
+	char buf[10];
+	FILE *f = popen("ps -C valknut | grep -c valknut", "r");
+	fread(buf, 1, 10, f);
+	pclose(f);
+	if(QString(buf).toInt() > 1)
+	{
+		printf("Process alredy exists.\n");
+
+		if (CAsyncDns::Instance())
+			delete CAsyncDns::Instance();
+		delete qApp;
+		if (g_pConfig)
+			delete g_pConfig;
+		if (CManager::Instance())
+			delete CManager::Instance();
+		delete g_pIconLoader;
+		g_pIconLoader = 0;
+
+		return 0;
+	}
+
 	if ( g_pConfig->LoadDCLib() != 0 )
 	{
 		ret = -1;
@@ -599,6 +664,8 @@
 
 	dclibDeInitDepLibs();
 
+	delete g_pWaitMagnet;
+
 	delete qApp;
 
 	printf("application exit ok %d\n",ret);
diff -Naur valknut-0.3.12/valknut/dcwaitmagnet.h valknut-0.3.12-mod/valknut/dcwaitmagnet.h
--- valknut-0.3.12/valknut/dcwaitmagnet.h	1970-01-01 07:00:00.000000000 +0700
+++ valknut-0.3.12-mod/valknut/dcwaitmagnet.h	2008-01-24 23:15:27.000000000 +0600
@@ -0,0 +1,45 @@
+//
+// C++ Interface: dcwaitmagnet
+//
+// Description: 
+//
+//
+// Author: Dmitry Krivomasov, (C) 2008
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+#ifndef DCWAITMAGNET_H
+#define DCWAITMAGNET_H
+
+#include <qwidget.h>
+#include <qfile.h>
+
+#include "dcgui.h"
+
+/**
+	@author Dmitry Krivomasov
+*/
+
+class DCWaitMagnet : public QWidget
+{
+  Q_OBJECT
+
+public:
+	DCWaitMagnet(DCGuiApp *gui);
+	~DCWaitMagnet();
+	void SearchByTTH(QString tth);
+protected:
+	void timerEvent(QTimerEvent *event);
+
+private:
+	int timerId;
+	void testFile();
+	QFile *file;
+	QString tth;
+	DCGuiApp *guiApp;
+};
+
+extern DCWaitMagnet * g_pWaitMagnet;
+
+#endif
diff -Naur valknut-0.3.12/valknut/dcwaitmagnet.cpp valknut-0.3.12-mod/valknut/dcwaitmagnet.cpp
--- valknut-0.3.12/valknut/dcwaitmagnet.cpp	1970-01-01 07:00:00.000000000 +0700
+++ valknut-0.3.12-mod/valknut/dcwaitmagnet.cpp	2008-01-24 23:15:27.000000000 +0600
@@ -0,0 +1,97 @@
+//
+// C++ Implementation: dcwaitmagnet
+//
+// Description: 
+//
+//
+// Author: Dmitry Krivomasov, (C) 2008
+//
+// Copyright: See COPYING file that comes with this distribution
+//
+//
+#include "dcwaitmagnet.h"
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#undef NDEBUG
+#include <assert.h>
+
+#include "dcconfig.h"
+#include "dchubsearch.h"
+
+#include <qcombobox.h>
+#include <qtabwidget.h>
+#include <qlineedit.h>
+
+DCWaitMagnet * g_pWaitMagnet = 0;
+
+DCWaitMagnet::DCWaitMagnet(DCGuiApp *gui)
+{
+	g_pWaitMagnet = this;
+	guiApp = gui;
+	file = new QFile((QString)g_pConfig->GetConfigPath().Data() + DIRSEPARATOR + "magnet");
+	timerId = startTimer(2000);
+}
+
+DCWaitMagnet::~DCWaitMagnet()
+{
+	killTimer(timerId);
+	delete file;
+	g_pWaitMagnet = NULL;
+}
+
+void DCWaitMagnet::timerEvent(QTimerEvent *event) 
+{
+	if (event->timerId() == timerId) { 
+		testFile();
+	} else {
+		QWidget::timerEvent(event);
+	}
+}
+
+void DCWaitMagnet::testFile()
+{
+	if(file->exists())
+	{
+		printf("New tth!!!\n");
+		if(file->open(IO_ReadOnly))
+		{
+			file->readLine(tth, 40);
+			file->close();
+			file->remove();
+			SearchByTTH(tth);
+		}
+	}
+}
+
+void DCWaitMagnet::SearchByTTH(QString tth)
+{
+
+	g_pHubSearch->Combobox_SEARCH->setEditText(tth);
+	g_pHubSearch->LineEdit_SEARCHSIZE->setText("0");
+	g_pHubSearch->ComboBox_SEARCHTYPE->setCurrentItem(9);
+	g_pHubSearch->ComboBox_SEARCHUNIT->setCurrentItem(0);
+	g_pHubSearch->ComboBox_SEARCHLIMIT->setCurrentItem(0);
+
+// Fixme: Search window is not always start showing
+
+	g_pHubSearch->setEnabled(TRUE);
+	if ( g_pHubSearch->isMinimized() )
+		g_pHubSearch->showNormal();
+	else if ( g_pHubSearch->isVisible() )
+		g_pHubSearch->setFocus();
+	else if ( g_pHubSearch->isMaximized() )
+		g_pHubSearch->showMaximized();
+	else
+		g_pHubSearch->show();
+
+//	g_pHubSearch->raise();
+//	g_pHubSearch->setActiveWindow();
+
+	g_pHubSearch->startSearchClick();
+
+	guiApp->ShowMainWindow();
+	guiApp->setFocus();
+}
diff -Naur valknut-0.3.12/valknut/dchubsearch.h valknut-0.3.12-mod/valknut/dchubsearch.h
--- valknut-0.3.12/valknut/dchubsearch.h	2007-02-17 19:45:04.000000000 +0600
+++ valknut-0.3.12-mod/valknut/dchubsearch.h	2008-01-24 23:15:27.000000000 +0600
@@ -100,6 +100,8 @@
 	/** To start search from another window prompting if another search is running */
 	void StartSearchWithPrompt();
 
+	void startSearchClick();
+
 protected:
 	/** Adjust column sizes preserving user set size ratios */
 	void SizeColumnsPreservingRatios();
diff -Naur valknut-0.3.12/valknut/dchubsearch.cpp valknut-0.3.12-mod/valknut/dchubsearch.cpp
@@ -1109,6 +1109,23 @@
 }
 
 /** */
+void DCHubSearch::startSearchClick()
+{
+	
+	for (int i=0; i<10; i++)
+	{
+		m_SearchManager.StopSearch();
+		PushButton_SEARCH->setEnabled(FALSE);
+		if ( m_SearchManager.SearchType() == estyNONE )
+		{
+			startSearch();
+			return;
+		}
+		usleep(100);
+	}
+}
+
+/** */
 void DCHubSearch::startSearch()
 {
 	CObject * Object;
diff -Naur valknut-0.3.12/valknut/dcgui.h valknut-0.3.12-mod/valknut/dcgui.h
--- valknut-0.3.12/valknut/dcgui.h	2007-08-13 19:19:12.000000000 +0700
+++ valknut-0.3.12-mod/valknut/dcgui.h	2008-01-24 23:15:27.000000000 +0600
@@ -63,6 +63,7 @@
 
 	/** */
 	void ShowOptionsDialog() { slotFileOptions(); };
+	void ShowMainWindow();
 	/** setup all windows */
 	void initWindows();
 	/** */
diff -Naur valknut-0.3.12/valknut/dcgui.cpp valknut-0.3.12-mod/valknut/dcgui.cpp
--- valknut-0.3.12/valknut/dcgui.cpp	2007-11-07 20:00:52.000000000 +0600
+++ valknut-0.3.12-mod/valknut/dcgui.cpp	2008-01-24 23:15:27.000000000 +0600
@@ -1517,6 +1517,40 @@
 	statusBar()->message(tr("Ready."));
 }
 
+/** undock the application (for activate when new magnet-link found) */
+void DCGuiApp::ShowMainWindow()
+{
+	if ( m_bTray )
+	{
+		if ( m_bWasMaximized )
+		{
+			showMaximized();
+		}
+		else
+		{
+			show();
+		}
+		// show dcgui in front of any applications after undock
+		raise();
+		// restore desktop position
+		move(m_DesktopPosition);
+
+		// restore transferview settings
+		//if ( m_bTransferViewDockVisible )
+		//{
+		//	pTransferViewDock->show();
+		//}
+
+		m_bTray = FALSE;
+	}
+	else
+	{
+		showNormal();
+	}
+
+	statusBar()->message(tr("Ready."));
+}
+
 /** dock the application */
 void DCGuiApp::slotViewDock()
 {
openSUSE Build Service is sponsored by