summaryrefslogtreecommitdiffstats
path: root/src/kasablanca.cpp
blob: 8895be453c405d139012609c51623e36b4a43b23 (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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
/***************************************************************************
                          kasablanca.cpp  -  description
                             -------------------
    begin                : Die Aug 19 20:54:14 CEST 2003
    copyright            : (C) 2003 by mkulke
    email                : sikor_sxe@radicalapproach.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kaction.h>
#include <kapp.h>
#include <kapplication.h>
#include <kdeversion.h>
#include <kedittoolbar.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <kicontheme.h>
#include <kkeydialog.h>
#include <tdelocale.h>
#include <tdemainwindow.h>
#include <kmenubar.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <kstatusbar.h>
#include <kstdaction.h>
#include <ksystemtray.h>
#include <kconfigdialog.h>
#include <kpassivepopup.h>
#include <klineedit.h>

#include <ntqtextedit.h>
#include <ntqcolor.h>
#include <ntqlistview.h>
#include <ntqpushbutton.h>
#include <ntqheader.h>
#include <ntqtoolbutton.h>
#include <ntqaction.h>

#include "Q_colorspreferencesdialog.h"
#include "Q_generalpreferencesdialog.h"
#include "Q_userinterfacepreferencesdialog.h"
#include "importdialog.h"
#include "customconnectdialog.h"
#include "fileexistsdialog.h"
#include "bookmarkdialog.h"
#include "kbtransferdir.h"
#include "kbtransferfile.h"
#include "kbdirinfo.h"
#include "ftpsession.h"
#include "kbconfig.h"
#include "kbtaskview.h"
#include "kasablanca.h"
#include "kbstatustip.h"

using namespace std;

Kasablanca::Kasablanca() : TDEMainWindow( 0, "Kasablanca" ), mp_view(new KasablancaMainWindow(this))
{
	//first, load the system tray icon
	mp_systemtray = new KSystemTray(this);
	mp_systemtray->setPixmap( KSystemTray::loadIcon( "kasablanca" ) );
	if ( KbConfig::systrayIsEnabled() ) mp_systemtray->show();
	
	mp_statustip = new KbStatusTip(mp_systemtray);
	
	mp_session_a = new FtpSession(this, "session a");
	mp_session_b = new FtpSession(this, "session b");
	mp_sessionlist = new list<FtpSession*>;
	mp_sessionlist->push_back(mp_session_a);
	mp_sessionlist->push_back(mp_session_b);

	setAcceptDrops(true);
	setCentralWidget(mp_view);
	setupGUI();
	setupActions();
	setupStatusBar();
	setupMenu();
	statusBar()->show();

	setAutoSaveSettings();

	/* when the qmainwindow is closed the object gets deleted,
	so the destructers of kbprocesses are called. */

	setWFlags(WDestructiveClose);

	/* if initbookmarks returns false, an error box appears */

	InitBookmarks();

	//if (!InitBookmarks() != 1) KMessageBox::error(0, i18n("Could not open Kasablanca bookmark xml."));

	connect(mp_view->BrowserB, SIGNAL(selectionChanged()), SLOT(SLOT_SelectionChanged()));
	connect(mp_view->BrowserA, SIGNAL(selectionChanged()), SLOT(SLOT_SelectionChanged()));
	connect(mp_view->TaskView, SIGNAL(selectionChanged()), SLOT(SLOT_SelectionChanged()));

	mp_session_a->SetLogWindow(mp_view->LogWindowA);
	mp_session_a->SetBrowser(mp_view->BrowserA);
	mp_session_a->SetConnectButton(mp_view->ConnectButtonA);
	mp_session_a->SetRefreshButton(mp_view->RefreshButtonA);
	mp_session_a->SetTransferButton(mp_view->TransferButtonA);
	mp_session_a->SetCmdLine(mp_view->CommandLineA);
	mp_session_a->SetCwdLine(mp_view->CwdLineA);
	mp_session_a->SetBookmarksMenu(&m_bookmarksmenu_a);
	mp_session_a->SetStatusLine(mp_statusline_a);
	mp_session_a->SetEncryptionIcon(mp_encryptionicon_a);
	mp_session_a->SetRclickMenu(&m_rclickmenu_a);
	mp_session_a->SetSessionList(mp_sessionlist);

	connect(mp_view->RefreshButtonA, SIGNAL(clicked()), mp_session_a, SLOT(SLOT_RefreshButton()));
	connect(mp_view->ConnectButtonA, SIGNAL(clicked()), mp_session_a, SLOT(SLOT_ConnectButton()));
	connect(mp_view->TransferButtonA, SIGNAL(clicked()), mp_session_a, SLOT(SLOT_TransferButton()));
	connect(mp_view->CwdLineA, SIGNAL(returnPressed()), mp_session_a, SLOT(SLOT_CwdLine()));
	connect(mp_view->CommandLineA, SIGNAL(returnPressed()), mp_session_a, SLOT(SLOT_CmdLine()));

	mp_session_b->SetLogWindow(mp_view->LogWindowB);
	mp_session_b->SetBrowser(mp_view->BrowserB);
	mp_session_b->SetConnectButton(mp_view->ConnectButtonB);
	mp_session_b->SetRefreshButton(mp_view->RefreshButtonB);
	mp_session_b->SetTransferButton(mp_view->TransferButtonB);
	mp_session_b->SetCmdLine(mp_view->CommandLineB);
	mp_session_b->SetCwdLine(mp_view->CwdLineB);
	mp_session_b->SetBookmarksMenu(&m_bookmarksmenu_b);
	mp_session_b->SetStatusLine(mp_statusline_b);
	mp_session_b->SetEncryptionIcon(mp_encryptionicon_b);
	mp_session_b->SetRclickMenu(&m_rclickmenu_b);
	mp_session_b->SetSessionList(mp_sessionlist);

	connect(mp_view->RefreshButtonB, SIGNAL(clicked()), mp_session_b, SLOT(SLOT_RefreshButton()));
	connect(mp_view->ConnectButtonB, SIGNAL(clicked()), mp_session_b, SLOT(SLOT_ConnectButton()));
	connect(mp_view->TransferButtonB, SIGNAL(clicked()), mp_session_b, SLOT(SLOT_TransferButton()));
	connect(mp_view->CwdLineB, SIGNAL(returnPressed()), mp_session_b, SLOT(SLOT_CwdLine()));
	connect(mp_view->CommandLineB, SIGNAL(returnPressed()), mp_session_b, SLOT(SLOT_CmdLine()));
	connect(mp_session_a, SIGNAL(gui_update()), SLOT(SLOT_SelectionChanged()));
	connect(mp_session_b, SIGNAL(gui_update()), SLOT(SLOT_SelectionChanged()));
	connect(mp_session_a, SIGNAL(gui_queueitems(KbDirInfo*, FtpSession*, FtpSession*, bool)),
	 	SLOT(SLOT_QueueItems(KbDirInfo*, FtpSession*, FtpSession*, bool)));
	connect(mp_session_b, SIGNAL(gui_queueitems(KbDirInfo*, FtpSession*, FtpSession*, bool)),
	 	SLOT(SLOT_QueueItems(KbDirInfo*, FtpSession*, FtpSession*, bool)));
	connect(mp_session_a, SIGNAL(gui_succeedtransfer(TQListViewItem*)), SLOT(SLOT_NextTransfer(TQListViewItem*)));
	connect(mp_session_b, SIGNAL(gui_succeedtransfer(TQListViewItem*)), SLOT(SLOT_NextTransfer(TQListViewItem*)));
	connect(mp_session_a, SIGNAL(gui_clearqueue(FtpSession*)), SLOT(SLOT_ClearQueue(FtpSession*)));
	connect(mp_session_b, SIGNAL(gui_clearqueue(FtpSession*)), SLOT(SLOT_ClearQueue(FtpSession*)));
	
	TQAction *deleteShortcut = new TQAction( TQPixmap(), "&Delete",
                                      Key_Delete, this, "delete" );
									  
	connect(deleteShortcut, SIGNAL(activated()), this, SLOT(SLOT_SkipTasks()));
	
	mp_session_a->Disconnect();
	mp_session_b->Disconnect();

	applyConfig();
}

void Kasablanca::setupGUI()
{
	mp_view->TaskView->setSorting(-1);
	mp_view->BrowserA->setSorting(-1);
	mp_view->BrowserB->setSorting(-1);

	mp_view->TransferButtonA->setIconSet(TDEGlobal::iconLoader()->loadIconSet("forward",TDEIcon::Toolbar));
	mp_view->TransferButtonB->setIconSet(TDEGlobal::iconLoader()->loadIconSet("back",TDEIcon::Toolbar));
	mp_view->RefreshButtonA->setIconSet(TDEGlobal::iconLoader()->loadIconSet("reload",TDEIcon::Toolbar));
	mp_view->RefreshButtonB->setIconSet(TDEGlobal::iconLoader()->loadIconSet("reload",TDEIcon::Toolbar));
	mp_view->ConnectButtonA->setIconSet(TDEGlobal::iconLoader()->loadIconSet("connect_no",TDEIcon::Toolbar));
	mp_view->ConnectButtonB->setIconSet(TDEGlobal::iconLoader()->loadIconSet("connect_no",TDEIcon::Toolbar));
}

void Kasablanca::applyConfig()
{
	FtpSession::filecheck def = FtpSession::off;
	if (!KbConfig::onFileExistsIsEnabled()) def = FtpSession::off;
	else if(KbConfig::onFileExistsOverwrite()) def = FtpSession::clear;
	else if(KbConfig::onFileExistsResume()) def = FtpSession::resume;
	else if(KbConfig::onFileExistsSkip()) def = FtpSession::skip;

	m_skiplist.setPattern(KbConfig::skiplist());
	m_skiplistenabled = KbConfig::skiplistIsEnabled();
	m_onqueuefinishedenabled = KbConfig::onQueueFinishedIsEnabled();
	m_onqueuefinished = KbConfig::onQueueFinished();

	list<FtpSession*>::iterator end_session = mp_sessionlist->end();
	for (list<FtpSession*>::iterator i = mp_sessionlist->begin(); i != end_session; i++)
	{
		(*i)->SetColors(KbConfig::localColor(), KbConfig::successColor(), KbConfig::failureColor(), KbConfig::backgroundColor());
		(*i)->SetOnFileExistsDefault(def);
		(*i)->EnableCmdLine(KbConfig::commandLineIsEnabled());
		(*i)->SetFont(KbConfig::logwindowFont());
		(*i)->FilterHiddenFiles(KbConfig::hideHiddenFilesIsEnabled());
	}

	// make the system tray switch on/off when settings are changed
	if (KbConfig::systrayIsEnabled()) mp_systemtray->show();
	else mp_systemtray->hide();
}

void Kasablanca::saveSettings()
{
	KbConfig::writeConfig();
}

void Kasablanca::setupActions()
{
	KStdAction::quit(kapp, SLOT(quit()), actionCollection());

	//m_toolbarAction = KStdAction::showToolbar(this, SLOT(optionsShowToolbar()), actionCollection());
	m_statusbarAction = KStdAction::showStatusbar(this, SLOT(optionsShowStatusbar()), actionCollection());

	KStdAction::keyBindings(this, SLOT(optionsConfigureKeys()), actionCollection());
	//KStdAction::configureToolbars(this, SLOT(optionsConfigureToolbars()), actionCollection());
	KStdAction::preferences(this, SLOT(optionsPreferences()), actionCollection());

	/*
	// this doesn't do anything useful.  it's just here to illustrate
	// how to insert a custom menu and menu item
	KAction *custom = new KAction(i18n("Cus&tom Menuitem"), 0,
											this, SLOT(optionsPreferences()),
											actionCollection(), "custom_action");
	*/

	new KAction(i18n("Edit"), CTRL+Key_E, this, SLOT(SLOT_EditBookmarks()), actionCollection(), "edit bookmarks action");

	new KAction(i18n("Import"), CTRL+Key_I, this, SLOT(SLOT_ImportBookmarks()), actionCollection(), "import bookmarks action");

	createGUI();
}

void Kasablanca::optionsShowToolbar()
{
    if (m_toolbarAction->isChecked())
        toolBar()->show();
    else
        toolBar()->hide();
}

void Kasablanca::optionsShowStatusbar()
{
    if (m_statusbarAction->isChecked())
        statusBar()->show();
    else
        statusBar()->hide();
}

void Kasablanca::optionsConfigureKeys()
{
    KKeyDialog::configure(actionCollection(), mp_view);
}

void Kasablanca::optionsConfigureToolbars()
{
    // use the standard toolbar editor
#if defined(KDE_MAKE_VERSION)
# if KDE_VERSION >= KDE_MAKE_VERSION(3,1,0)
    saveMainWindowSettings(TDEGlobal::config(), autoSaveGroup());
# else
    saveMainWindowSettings(TDEGlobal::config());
# endif
#else
    saveMainWindowSettings(TDEGlobal::config());
#endif
}

void Kasablanca::optionsPreferences()
{
	if (TDEConfigDialog::showDialog("settings")) return;

	TDEConfigDialog* dialog = new TDEConfigDialog(0, "settings", KbConfig::self(), KDialogBase::IconList,
		KDialogBase::Ok | KDialogBase::Apply | KDialogBase::Cancel | KDialogBase::Help);

	dialog->setHelp("globalhelp");

	KasablancaGeneralPreferencesDialog *general = new KasablancaGeneralPreferencesDialog(0, i18n("General"));
	KasablancaColorsPreferencesDialog *colors = new KasablancaColorsPreferencesDialog(0, i18n("Colors"));
	KasablancaUserInterfacePreferencesDialog *ui = new KasablancaUserInterfacePreferencesDialog(0, i18n("User Interface"));
	dialog->addPage(general, i18n("General"), "kasablanca");
	dialog->addPage(colors, i18n("Colors"), "colors");
	dialog->addPage(ui, i18n("User Interface"), "winprops");
	connect(dialog, SIGNAL(settingsChanged()), this, SLOT(applyConfig()));
	dialog->show();
}

void Kasablanca::newToolbarConfig()
{
    // this slot is called when user clicks "Ok" or "Apply" in the toolbar editor.
    // recreate our GUI, and re-apply the settings (e.g. "text under icons", etc.)
    createGUI();

#if defined(KDE_MAKE_VERSION)
# if KDE_VERSION >= KDE_MAKE_VERSION(3,1,0)
    applyMainWindowSettings(TDEGlobal::config(), autoSaveGroup());
# else
    applyMainWindowSettings(TDEGlobal::config());
# endif
#else
    applyMainWindowSettings(TDEGlobal::config());
#endif
}

void Kasablanca::setupMenu()
{
	TQWidget *w;
	w = factory()->container("connect menu", this);
	if (w == NULL) tqWarning("ERROR: 'connect menu' not found in .ui file");
	else
	{
		TQPopupMenu *menu = static_cast<TQPopupMenu *>(w);
		menu->insertItem(i18n("Session A"),  &m_bookmarksmenu_a);
		menu->insertItem(i18n("Session B"), &m_bookmarksmenu_b);
	}
	w = factory()->container("action menu", this);
	if (w == NULL) tqWarning("ERROR: 'action menu' not found in .ui file");
	else
	{
		TQPopupMenu *menu = static_cast<TQPopupMenu *>(w);
		menu->insertItem(i18n("Session A"),  &m_rclickmenu_a);
		menu->insertItem(i18n("Session B"), &m_rclickmenu_b);
		menu->insertItem(i18n("Queue"), &m_rclickmenu_t);
	}

	m_rclickmenu_a.insertItem(i18n("Transfer"), Transfer);
	m_rclickmenu_a.insertItem(i18n("Put in queue"), Queue);
	m_rclickmenu_a.insertSeparator();
	m_rclickmenu_a.insertItem(i18n("Delete"), Delete);
	m_rclickmenu_a.insertItem(i18n("Rename"), Rename);
	m_rclickmenu_a.insertItem(i18n("Mkdir"), Mkdir);
	m_rclickmenu_a.insertSeparator();
	m_rclickmenu_a.insertItem(i18n("Bookmark site"), Bookmark);

	m_rclickmenu_b.insertItem(i18n("Transfer"), Transfer);
	m_rclickmenu_b.insertItem(i18n("Put in queue"), Queue);
	m_rclickmenu_b.insertSeparator();
	m_rclickmenu_b.insertItem(i18n("Delete"), Delete);
	m_rclickmenu_b.insertItem(i18n("Rename"), Rename);
	m_rclickmenu_b.insertItem(i18n("Mkdir"), Mkdir);
	m_rclickmenu_b.insertSeparator();
	m_rclickmenu_b.insertItem(i18n("Bookmark site"), Bookmark);

	m_rclickmenu_t.insertItem(i18n("Start Queue"), Start);
	m_rclickmenu_t.insertSeparator();
	m_rclickmenu_t.insertItem(i18n("Skip Item(s)"), Skip);

	connect(&m_rclickmenu_a, SIGNAL(activated(int)), mp_session_a, SLOT(SLOT_ActionMenu(int)));
	connect(&m_bookmarksmenu_a, SIGNAL(activated(int)), mp_session_a, SLOT(SLOT_ConnectMenu(int)));
	connect(mp_view->BrowserA, SIGNAL(doubleClicked(TQListViewItem*)), mp_session_a, SLOT(SLOT_ItemClicked(TQListViewItem*)));
	connect(mp_view->BrowserA, SIGNAL(rightButtonPressed(TQListViewItem *, const TQPoint &, int)),
		mp_session_a, SLOT(SLOT_ItemRClicked(TQListViewItem *, const TQPoint &, int)));
	connect(mp_view->BrowserA->header(), SIGNAL (clicked(int)), mp_session_a, SLOT (SLOT_HeaderClicked(int)));

	connect(&m_rclickmenu_b, SIGNAL(activated(int)), mp_session_b, SLOT(SLOT_ActionMenu(int)));
	connect(&m_bookmarksmenu_b, SIGNAL(activated(int)), mp_session_b, SLOT(SLOT_ConnectMenu(int)));
	connect(mp_view->BrowserB, SIGNAL(doubleClicked(TQListViewItem*)), mp_session_b, SLOT(SLOT_ItemClicked(TQListViewItem*)));
	connect(mp_view->BrowserB, SIGNAL(rightButtonPressed(TQListViewItem *, const TQPoint &, int)),
		mp_session_b, SLOT(SLOT_ItemRClicked(TQListViewItem *, const TQPoint &, int)));
	connect(mp_view->BrowserB->header(), SIGNAL(clicked(int)), mp_session_b, SLOT(SLOT_HeaderClicked(int)));

	connect( mp_view->TaskView, SIGNAL (rightButtonPressed( TQListViewItem *, const TQPoint &, int )), this,
			SLOT (SLOT_ItemRightClickedT(TQListViewItem *, const TQPoint &, int )));

	m_rclickmenu_t.connectItem(Start, this, SLOT(SLOT_ProcessQueue()));
	m_rclickmenu_t.connectItem(Skip, this, SLOT(SLOT_SkipTasks()));
}

void Kasablanca::setupStatusBar()
{
    m_iconencrypted = TDEGlobal::iconLoader()->loadIconSet("encrypted",TDEIcon::Small).pixmap(TQIconSet::Small,TQIconSet::Normal);
    m_iconunencrypted = TDEGlobal::iconLoader()->loadIconSet("encrypted",TDEIcon::Small).pixmap(TQIconSet::Small,TQIconSet::Disabled);

    mp_statusline_a = new TQLabel("", statusBar());
    mp_encryptionicon_a = new TQLabel(statusBar());
    mp_encryptionicon_a->setPixmap(m_iconunencrypted);

    mp_statusline_b = new TQLabel("", statusBar());
    mp_encryptionicon_b = new TQLabel(statusBar());
    mp_encryptionicon_b->setPixmap(m_iconunencrypted);

    statusBar()->addWidget(mp_statusline_a, 1);
    statusBar()->addWidget(mp_encryptionicon_a, 0);
    statusBar()->addWidget(mp_statusline_b, 1, true);
    statusBar()->addWidget(mp_encryptionicon_b, 0, true);
}

Kasablanca::~Kasablanca()
{
}

void Kasablanca::InitBookmarks()
{
	int count;
	m_bookmarks.clear();
	m_bookmarks = KbSiteInfo::ParseBookmarks();

	m_bookmarksmenu_a.clear();
	m_bookmarksmenu_b.clear();

	m_bookmarksmenu_a.insertItem(i18n("Custom"),0);
	m_bookmarksmenu_b.insertItem(i18n("Custom"),0);
	count = 1;

	m_bookmarksmenu_a.insertSeparator();
	m_bookmarksmenu_b.insertSeparator();

	list<KbSiteInfo>::iterator end_bookmarks = m_bookmarks.end();
	for (list<KbSiteInfo>::iterator i = m_bookmarks.begin(); i != end_bookmarks; i++)
	{
		m_bookmarksmenu_a.insertItem((*i).GetName(),count);
		m_bookmarksmenu_b.insertItem((*i).GetName(),count);
		count++;
	}
}

void Kasablanca::SLOT_QueueItems(KbDirInfo *dir, FtpSession* src, FtpSession* dst, bool startqueue)
{
	QueueItemsRecurse(dir, src, dst);
	if ((mp_view->TaskView->firstChild()) && (startqueue))
	{
		if (!src->Connected()) ProcessQueue(static_cast<KbTransferItem*>(mp_view->TaskView->firstChild()));
		else static_cast<KbTransferItem*>(mp_view->TaskView->firstChild())->Init();
	}
}

void Kasablanca::QueueItemsRecurse(KbDirInfo *dir, FtpSession* src, FtpSession* dst, TQListViewItem* parent)
{
	list<KbFileInfo*>* filelist;
	list<KbDirInfo*>* dirlist;
	TQListViewItem *after, *lastchild;

	filelist = dir->Filelist();
	dirlist = dir->Dirlist();

	if (KbConfig::prioritylistIsEnabled())
	{
		dirlist->sort(KbDirInfo::PrioritySort);
		filelist->sort(KbFileInfo::PrioritySort);
	}

	lastchild = mp_view->TaskView->LastChild();

	after = NULL;
	list<KbFileInfo*>::iterator end_file = filelist->end();
	for(list<KbFileInfo*>::iterator fileIterator = filelist->begin(); fileIterator != end_file; fileIterator++)
	{
		if ((m_skiplist.search((*fileIterator)->fileName()) < 0) || (!m_skiplistenabled))
		{
			KbFileInfo *srcfi = new KbFileInfo(**fileIterator);
			KbFileInfo *dstfi = new KbFileInfo(**fileIterator);
			srcfi->SetDirPath(src->WorkingDir() + srcfi->dirPath());
			dstfi->SetDirPath(dst->WorkingDir() + dstfi->dirPath());
			if (parent) after = new KbTransferFile(parent, after, src, dst, srcfi, dstfi);
			else new KbTransferFile(mp_view->TaskView, lastchild /*mp_view->TaskView->lastItem()*/, src, dst, srcfi, dstfi);
		}
		else tqWarning("INFO: entry ignored due to matched skiplist regexp");
	}

	after = NULL;
	list<KbDirInfo*>::iterator end_dir = dirlist->end();
	for(list<KbDirInfo*>::iterator dirIterator = dirlist->begin(); dirIterator != end_dir; dirIterator++)
	{
		if ((m_skiplist.search((*dirIterator)->fileName()) < 0) || (!m_skiplistenabled))
		{
			KbFileInfo *srcfi = new KbFileInfo(*(*dirIterator));
			KbFileInfo *dstfi = new KbFileInfo(*(*dirIterator));
			srcfi->SetDirPath(src->WorkingDir() + srcfi->dirPath());
			dstfi->SetDirPath(dst->WorkingDir() + dstfi->dirPath());
			if (parent) after = new KbTransferDir(parent, after, src, dst, srcfi, dstfi);
			else after = new KbTransferDir(mp_view->TaskView, lastchild /*mp_view->TaskView->lastItem()*/, src, dst, srcfi, dstfi);
			QueueItemsRecurse(*dirIterator, src, dst, after);
		}
		else tqWarning("INFO: entry ignored due to matched skiplist regexp");
	}
	delete dir;
}

void Kasablanca::SLOT_EditBookmarks()
{
    BookmarkDialog dlg;

    if (dlg.exec() == TQDialog::Accepted)
    {
        InitBookmarks();
    }
}

void Kasablanca::SLOT_ImportBookmarks()
{
	ImportDialog dlg;
	dlg.exec();

	int cnt=0;
	list<KbSiteInfo> newbookmarklist;

	list<KbSiteInfo>::iterator end_session = dlg.Bookmarks()->end();
	for (list<KbSiteInfo>::iterator i = dlg.Bookmarks()->begin(); i != end_session; i++)
	{
		m_bookmarks.push_back( *i );
		cnt++;
	}
	KbSiteInfo::WriteBookmarks(m_bookmarks);
}

void Kasablanca::SLOT_ClearQueue(FtpSession*)
{
	mp_view->TaskView->clear();
}

void Kasablanca::SLOT_ItemRightClickedT(TQListViewItem *, const TQPoint & point, int)
{
	m_rclickmenu_t.exec(point);
}

void Kasablanca::SLOT_SkipTasks()
{
    TQListViewItemIterator it(mp_view->TaskView);
    while ( it.current() )
    {
        if (it.current()->isSelected()) delete it.current();
        else ++it;
    }
}

void Kasablanca::SLOT_SelectionChanged()
{
	int counter_a = 0;
	int counter_b = 0;
	bool flag;

	TQListViewItemIterator ita(mp_view->BrowserA);
 	while ( ita.current() )
 	{
   	if (ita.current()->isSelected()) counter_a++;
   	++ita;
 	}

	flag = ((counter_a) && (!mp_session_a->Occupied()) && (!mp_session_b->Occupied()));

	m_rclickmenu_a.setItemEnabled(Transfer, flag);
	m_rclickmenu_a.setItemEnabled(Queue, flag);
	mp_view->TransferButtonA->setEnabled(flag);

	/* when at least one item is selected, Delete and Rename are
	enabled. */

	m_rclickmenu_a.setItemEnabled(Rename, (counter_a >= 1));
	m_rclickmenu_a.setItemEnabled(Delete, (counter_a >= 1));

	TQListViewItemIterator itb(mp_view->BrowserB);
 	while ( itb.current() )
 	{
   	if (itb.current()->isSelected()) counter_b++;
   	++itb;
 	}

	flag = ((counter_b) && (!mp_session_a->Occupied()) && (!mp_session_b->Occupied()));

	m_rclickmenu_b.setItemEnabled(Transfer, flag);
	m_rclickmenu_b.setItemEnabled(Queue, flag);
	mp_view->TransferButtonB->setEnabled(flag);

	/* when at least one item is selected, Delete and Rename are
	enabled. */

	m_rclickmenu_b.setItemEnabled(Rename, (counter_b >= 1));
	m_rclickmenu_b.setItemEnabled(Delete, (counter_b >= 1));
}

void Kasablanca::SLOT_NextTransfer(TQListViewItem* item)
{	
	TQListViewItem *next;
	next = NextTransfer(item);
	if (next) ProcessQueue(static_cast<KbTransferItem*>(next));
	else QueueFinished();
}

void Kasablanca::QueueFinished()
{
	if (m_onqueuefinishedenabled)
	{
		TDEProcess* p = new TDEProcess();
		*p << TQStringList::split(" ", m_onqueuefinished);
		connect(p, SIGNAL(processExited(TDEProcess*)), SLOT(SLOT_LocalProcessExited(TDEProcess*)));
		p->start();
	}
	if (KbConfig::systrayIsEnabled()) KPassivePopup::message("Transfer is finished.", mp_systemtray);
	//TQToolTip::remove(mp_systemtray);
}

TQListViewItem* Kasablanca::NextTransfer(TQListViewItem* item)
{
	FtpSession *src, *dst;
	TQListViewItem *parent, *next, *retval;
	src = static_cast<KbTransferItem*>(item)->SrcSession();
	dst = static_cast<KbTransferItem*>(item)->DstSession();
	next = item->nextSibling();
	parent = item->parent();
	retval = NULL;
	if (item->childCount() == 0) // if the item contains no subelements - delete it, if it doesn't - start with the next
	{
		static_cast<KbTransferItem*>(item)->Finish();
		if (mp_view->TaskView->childCount() > 0) delete item;
		if (next) retval = next;
		else if (parent) retval = NextTransfer(parent);
		else
		{
			src->SLOT_RefreshButton();
			dst->SLOT_RefreshButton();
		}
	}
	else retval = item->firstChild();

	return retval;
}

void Kasablanca::SLOT_ProcessQueue()
{
	KbTransferItem *ti = static_cast<KbTransferItem*>(mp_view->TaskView->firstChild());
	if (ti) ProcessQueue(ti);
}

void Kasablanca::ProcessQueue(KbTransferItem* item)
{
	FtpSession *srcsession = item->SrcSession();
	srcsession->Transfer(item);
}