summaryrefslogtreecommitdiffstats
path: root/plugins/scanfolder/scanfolderplugin.cpp
blob: a994243c9a92ea30f12bc482d956e050df194826 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/***************************************************************************
 *   Copyright (C) 2006 by Ivan Vasić                                      *
 *   ivasic@gmail.com                                                      *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.           *
 ***************************************************************************/
#include <kgenericfactory.h>

#include <interfaces/coreinterface.h>
#include <interfaces/guiinterface.h>
#include <interfaces/plugin.h>
#include <util/constants.h>
#include <util/log.h>

#include <qstring.h>
#include <qfile.h>

#include <kmessagebox.h>
#include <klocale.h>
#include <kglobal.h>

#include "scanfolder.h"
#include "scanfolderplugin.h"
#include "scanfolderprefpage.h"
#include "scanfolderpluginsettings.h"

using namespace bt;

K_EXPORT_COMPONENT_FACTORY(ktscanfolderplugin,KGenericFactory<kt::ScanFolderPlugin>("scanfolderplugin"))

namespace kt
{	
	const QString NAME = "Scan Folder";
	const QString AUTHOR = "Ivan Vasic";
	const QString EMAIL = "ivasic@gmail.com";
	const QString DESCRIPTION = i18n("Automatically scans directories for torrent files and loads them.");

	ScanFolderPlugin::ScanFolderPlugin(QObject* parent, const char* name, const QStringList& args)
	: Plugin(parent, name, args,NAME,i18n("Scan Folder"),AUTHOR,EMAIL,DESCRIPTION,"view_sidetree")
	{
// 		setXMLFile("ktscanfolderpluginui.rc");
		m_sf1 = 0;
		m_sf2 = 0;
		m_sf3 = 0;
	}


	ScanFolderPlugin::~ScanFolderPlugin()
	{
	}

	void ScanFolderPlugin::load()
	{
		pref = new ScanFolderPrefPage(this);
		getGUI()->addPrefPage(pref);
		updateScanFolders();
	}

	void ScanFolderPlugin::unload()
	{
		getGUI()->removePrefPage(pref);
		delete pref;
		pref = 0;
		
		if(m_sf1)
			delete m_sf1;
		m_sf1 = 0;
		
		if(m_sf2)
			delete m_sf2;
		m_sf2 = 0;
		
		if(m_sf3)
			delete m_sf3;
		m_sf3 = 0;
	}
	
	void ScanFolderPlugin::updateScanFolders()
	{
		QString sfPath1 = ScanFolderPluginSettings::folder1();
		QString sfPath2 = ScanFolderPluginSettings::folder2();
		QString sfPath3 = ScanFolderPluginSettings::folder3();
		
		bool valid1 = QFile::exists(sfPath1);
		bool valid2 = QFile::exists(sfPath2);
		bool valid3 = QFile::exists(sfPath3);
		
		bool usesf1 = ScanFolderPluginSettings::useFolder1() && valid1;
		bool usesf2 = ScanFolderPluginSettings::useFolder2() && valid2;
		bool usesf3 = ScanFolderPluginSettings::useFolder3() && valid3;
		
		bool silently = ScanFolderPluginSettings::openSilently();
		
		LoadedTorrentAction action;
		
		if(ScanFolderPluginSettings::actionDelete())
			action = deleteAction;
		else if(ScanFolderPluginSettings::actionMove())
			action = moveAction;
		else
			action = defaultAction;
		
		
		if(usesf1)
		{
			if(!m_sf1)
				m_sf1 = new ScanFolder(getCore(), sfPath1, action, silently);
			else
			{
				m_sf1->setFolderUrl(sfPath1);
				m_sf1->setLoadedAction(action);
				m_sf1->setOpenSilently(silently);
			}
		}
		else
		{
			if(m_sf1)
				delete m_sf1;
			m_sf1 = 0;
		}
		
		if(usesf2)
		{
			if(!m_sf2)
				m_sf2 = new ScanFolder(getCore(), sfPath1, action, silently);
			else
			{
				m_sf2->setFolderUrl(sfPath1);
				m_sf2->setLoadedAction(action);
				m_sf2->setOpenSilently(silently);
			}
		}
		else
		{
			if(m_sf2)
				delete m_sf2;
			m_sf2 = 0;
		}
		
		if(usesf3)
		{
			if(!m_sf3)
				m_sf3 = new ScanFolder(getCore(), sfPath1, action, silently);
			else
			{
				m_sf3->setFolderUrl(sfPath1);
				m_sf3->setLoadedAction(action);
				m_sf3->setOpenSilently(silently);
			}
		}
		else
		{
			if(m_sf3)
				delete m_sf3;
			m_sf3 = 0;
		}
		
		//update config file
		if(!valid1)
			ScanFolderPluginSettings::setUseFolder1(false);
		if(!valid2)
			ScanFolderPluginSettings::setUseFolder2(false);
		if(!valid3)
			ScanFolderPluginSettings::setUseFolder3(false);
		
		ScanFolderPluginSettings::writeConfig();
			
	}
	
	bool ScanFolderPlugin::versionCheck(const QString & version) const
	{
		return version == KT_VERSION_MACRO;
	}
}