summaryrefslogtreecommitdiffstats
path: root/plugins/scheduler/bwsprefpagewidget.cpp
blob: 96e06130b3375d4084bc307290dc6c21ff5ac13f (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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/***************************************************************************
 *   Copyright (C) 2006 by Ivan Vasić                                      *
 *   [email protected]                                                      *
 *                                                                         *
 *   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 "bwspage.h"
#include "bwsprefpagewidget.h"
#include "bwscheduler.h"
#include "bwswidget.h"
#include "schedulerpluginsettings.h"

#include <knuminput.h>
#include <klocale.h>
#include <kglobal.h>
#include <kstandarddirs.h>
#include <kfiledialog.h>
#include <kmessagebox.h>
#include <kiconloader.h>
#include <kstdguiitem.h>
#include <kpushbutton.h>

#include <tqcombobox.h>
#include <tqfile.h>
#include <tqdatastream.h>
#include <tqlabel.h>
#include <tqcheckbox.h>
#include <tqradiobutton.h>

namespace kt
{

	/* PREF PAGE WIDGET --------------------------------------------------------*/

	BWSPrefPageWidget::BWSPrefPageWidget(TQWidget* tqparent, const char* name, WFlags fl)
			: BWSPage(tqparent,name,fl)
	{

		loadDefault();

		lbltqStatus->clear();

		pix_icon->setPixmap(KGlobal::iconLoader()->loadIcon("clock",KIcon::NoGroup));
		
		btnOk->setGuiItem(KStdGuiItem::ok());
		btnCancel->setGuiItem(KStdGuiItem::cancel());
		btnApply->setGuiItem(KStdGuiItem::apply());
		btnSave->setGuiItem(KStdGuiItem::saveAs());
		btnLoad->setGuiItem(KStdGuiItem::open());
		btnReset->setGuiItem(KStdGuiItem::reset());
		

		bool use_colors = SchedulerPluginSettings::useColors();

		if(use_colors) //set up colors
		{
			pix1->setPaletteBackgroundColor(TQColor(30,165,105));
			pix2->setPaletteBackgroundColor(TQColor(195,195,70));
			pix3->setPaletteBackgroundColor(TQColor(195,195,70));
			pix4->setPaletteBackgroundColor(TQColor(195,195,70));
			pix5->setPaletteBackgroundColor(TQColor(190,30,30));
			
			pix12->setPaletteBackgroundColor(TQColor(30,165,105));
			pix22->setPaletteBackgroundColor(TQColor(195,195,70));
			pix32->setPaletteBackgroundColor(TQColor(195,195,70));
			pix42->setPaletteBackgroundColor(TQColor(195,195,70));
			pix52->setPaletteBackgroundColor(TQColor(190,30,30));
		} else //set up pixmaps
		{
			pix1->setPixmap(TQPixmap(locate("data", TQString("ktorrent/icons/cell-a-0000.png"))));
			pix2->setPixmap(TQPixmap(locate("data", TQString("ktorrent/icons/cell-a-0001.png"))));
			pix3->setPixmap(TQPixmap(locate("data", TQString("ktorrent/icons/cell-a-0002.png"))));
			pix4->setPixmap(TQPixmap(locate("data", TQString("ktorrent/icons/cell-a-0003.png"))));
			pix5->setPixmap(TQPixmap(locate("data", TQString("ktorrent/icons/cell-a-0004.png"))));

			pix12->setPixmap(TQPixmap(locate("data", TQString("ktorrent/icons/cell-a-0000.png"))));
			pix22->setPixmap(TQPixmap(locate("data", TQString("ktorrent/icons/cell-a-0001.png"))));
			pix32->setPixmap(TQPixmap(locate("data", TQString("ktorrent/icons/cell-a-0002.png"))));
			pix42->setPixmap(TQPixmap(locate("data", TQString("ktorrent/icons/cell-a-0003.png"))));
			pix52->setPixmap(TQPixmap(locate("data", TQString("ktorrent/icons/cell-a-0004.png"))));
		}

		connect(radio1, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(categoryChanged(int)));
		connect(radio2, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(categoryChanged(int)));
		connect(radio3, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(categoryChanged(int)));
		connect(radio4, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(categoryChanged(int)));
		connect(radio5, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(categoryChanged(int)));
		connect(radio12, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(categoryChanged(int)));
		connect(radio22, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(categoryChanged(int)));
		connect(radio32, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(categoryChanged(int)));
		connect(radio42, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(categoryChanged(int)));
		connect(radio52, TQT_SIGNAL(stateChanged(int)), this, TQT_SLOT(categoryChanged(int)));

		//pre-check default categories (say 1 for left and 0 for right)
		radio2->setChecked(true);
		radio12->setChecked(true);

		m_bwsWidget->setSchedule(schedule);
	}

	BWSPrefPageWidget::~BWSPrefPageWidget()
	{}

	void BWSPrefPageWidget::btnSave_clicked()
	{
		TQString sf = KFileDialog::getSaveFileName("/home","*",this,i18n("Choose a filename to save under"));

		if(sf.isEmpty())
			return;

		saveSchedule(sf);
	}

	void BWSPrefPageWidget::btnLoad_clicked()
	{
		TQString lf = KFileDialog::getOpenFileName("/home", "*",this,i18n("Choose a file"));

		if(lf.isEmpty())
			return;

		btnReset_clicked();
		loadSchedule(lf);
	}

	void BWSPrefPageWidget::saveSchedule(TQString& fn)
	{
		schedule = m_bwsWidget->schedule();

		TQFile file(fn);

		file.open(IO_WriteOnly);
		TQDataStream stream(&file);

		//First category
		stream << dlCat1->value();
		stream << ulCat1->value();

		//Second category
		stream << dlCat2->value();
		stream << ulCat2->value();

		//Third category
		stream << dlCat3->value();
		stream << ulCat3->value();

		//Now schedule
		for(int i=0; i<7; ++i)
			for(int j=0; j<24; ++j)
				stream << (int) schedule.getCategory(i, j);

		file.close();
		lbltqStatus->setText(i18n("Schedule saved."));
	}

	void BWSPrefPageWidget::loadSchedule(TQString& fn, bool showmsg)
	{
		TQFile file(fn);

		if(!file.exists()) {
			if(showmsg)
				KMessageBox::error(this, i18n("File not found."), i18n("Error"));
			return;
		}

		file.open(IO_ReadOnly);
		TQDataStream stream(&file);

		int tmp;

		stream >> tmp;
		dlCat1->setValue(tmp);
		stream >> tmp;
		ulCat1->setValue(tmp);

		stream >> tmp;
		dlCat2->setValue(tmp);
		stream >> tmp;
		ulCat2->setValue(tmp);

		stream >> tmp;
		dlCat3->setValue(tmp);
		stream >> tmp;
		ulCat3->setValue(tmp);

		for(int i=0; i<7; ++i) {
			for(int j=0; j<24; ++j) {
				stream >> tmp;
				schedule.setCategory(i, j, (ScheduleCategory) tmp);
			}
		}

		file.close();

		m_bwsWidget->setSchedule(schedule);
		lbltqStatus->setText(i18n("Schedule loaded."));
	}

	void BWSPrefPageWidget::loadDefault()
	{
		//read schedule from HD
		TQString fn = KGlobal::dirs()->saveLocation("data","ktorrent") + "bwschedule";
		loadSchedule(fn, false);
	}

	void BWSPrefPageWidget::btnReset_clicked()
	{
		schedule.reset();
		m_bwsWidget->resetSchedule();

		dlCat1->setValue(0);
		dlCat2->setValue(0);
		dlCat3->setValue(0);

		ulCat1->setValue(0);
		ulCat2->setValue(0);
		ulCat3->setValue(0);

		lbltqStatus->clear();
	}

	void BWSPrefPageWidget::apply()
	{
		schedule = m_bwsWidget->schedule();

		SchedulerPluginSettings::writeConfig();

		//update category values...
		schedule.setDownload(0, dlCat1->value());
		schedule.setUpload(0, ulCat1->value());
		schedule.setDownload(1, dlCat2->value());
		schedule.setUpload(1, ulCat2->value());
		schedule.setDownload(2, dlCat3->value());
		schedule.setUpload(2, ulCat3->value());

		//set new schedule
		BWScheduler::instance().setSchedule(schedule);
	}

	void BWSPrefPageWidget::btnOk_clicked()
	{
		apply();
		accept();
	}

	void BWSPrefPageWidget::btnApply_clicked()
	{
		apply();
	}
}

void kt::BWSPrefPageWidget::categoryChanged(int)
{
	if(radio1->isChecked())
		m_bwsWidget->setLeftCategory(0);
	else if(radio2->isChecked())
		m_bwsWidget->setLeftCategory(1);
	else if(radio3->isChecked())
		m_bwsWidget->setLeftCategory(2);
	else if(radio4->isChecked())
		m_bwsWidget->setLeftCategory(3);
	else if(radio5->isChecked())
		m_bwsWidget->setLeftCategory(4);

	if(radio12->isChecked())
		m_bwsWidget->setRightCategory(0);
	else if(radio22->isChecked())
		m_bwsWidget->setRightCategory(1);
	else if(radio32->isChecked())
		m_bwsWidget->setRightCategory(2);
	else if(radio42->isChecked())
		m_bwsWidget->setRightCategory(3);
	else if(radio52->isChecked())
		m_bwsWidget->setRightCategory(4);
}



#include "bwsprefpagewidget.moc"