summaryrefslogtreecommitdiffstats
path: root/kget/dlgDirectories.cpp
blob: 61688190f24ee6561d53f34beca1e8215a144b2f (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
/***************************************************************************
*                               dlgDirectories.cpp
*                             -------------------
*
*    Revision     : $Id$
*    begin        : Tue Jan 29 2002
*    copyright    : (C) 2002 by Patrick Charbonnier
*                 : Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
*    email        : [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.
 *
 ***************************************************************************/


#include <tqpushbutton.h>
#include <tqtoolbutton.h>
#include <tqlistview.h>

#include <tqdir.h>

#include <tdefiledialog.h>
#include <klineedit.h>
#include <kglobal.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kurlrequester.h>

#include "settings.h"
#include "dlgDirectories.h"
#include <kapplication.h>

DlgDirectories::DlgDirectories(TQWidget * parent)
    : DlgDirectoriesBase(parent)
{
    connect( le_ext, TQT_SIGNAL( textChanged ( const TQString & ) ), this,  TQT_SLOT( slotDirectoryChanged( ) ) );
    connect( le_dir, TQT_SIGNAL( textChanged ( const TQString & ) ), this,  TQT_SLOT( slotDirectoryChanged( ) ) );

    le_dir->setMode( KFile::Directory );
    lv_entries->setSortColumn( -1 );

    slotDirectoryChanged();
}

void DlgDirectories::slotDirectoryChanged( )
{
    pb_add->setEnabled(!le_ext->text().isEmpty() &&!le_dir->url().isEmpty() );
}

void DlgDirectories::selectEntry(TQListViewItem * item)
{
    if (item) {
        le_ext->setText(item->text(0));
        le_dir->setURL(item->text(1));

    } else {
        le_ext->clear();
        le_dir->clear();
    }
    updateUpDown();
}


void DlgDirectories::updateUpDown()
{
    TQListViewItem *item = lv_entries->selectedItem();

    pb_up->setEnabled( item && item->itemAbove() );
    pb_down->setEnabled( item && item->itemBelow() );
}

void DlgDirectories::addEntry()
{
    TQString ext = le_ext->text();
    TQString dir = le_dir->url();

    if (ext.contains(",") || dir.contains(",") || ext.isEmpty() || dir.isEmpty()) {
        KMessageBox::error(this, i18n("Each row consists of exactly one\nextension type and one folder."), i18n("Error"));
        return;
    }

    TQDir f(dir);

    if (!f.exists()) {
        KMessageBox::error(this, i18n("Folder does not exist:\n%1").arg(dir), i18n("Error"));
        return;
    }

    new TQListViewItem(lv_entries, ext, dir);
    updateUpDown();

    emit configChanged();
}


void DlgDirectories::deleteEntry()
{
    TQListViewItem *item = lv_entries->selectedItem();
    delete item;
    updateUpDown();
    emit configChanged();
}


void DlgDirectories::changeEntry()
{
    TQListViewItem *old_item = lv_entries->selectedItem();

    if (old_item) {
        TQString ext = le_ext->text();
        TQString dir = le_dir->url();

        if (ext.contains(",") || dir.contains(",") || ext.isEmpty() || dir.isEmpty()) {
            KMessageBox::error(this, i18n("Each row consists of exactly one\nextension type and one folder."), i18n("Error"));
            return;
        }

        TQDir f(dir);

        if (!f.exists()) {
            KMessageBox::error(this, i18n("Folder does not exist:\n%1").arg(dir), i18n("Error"));
            return;
        }

        new TQListViewItem(lv_entries, old_item, ext, dir);
        delete old_item;
        emit configChanged();
    }
}


void DlgDirectories::downEntry()
{
    TQListViewItem *item = lv_entries->selectedItem();

    if ( !item )
        return;

    item->moveItem( item->itemBelow() );

    updateUpDown();
    emit configChanged();
}


void DlgDirectories::upEntry()
{
    TQListViewItem *item = lv_entries->selectedItem();

    if ( !item || !item->itemAbove() )
        return;

    item->moveItem( item->itemAbove()->itemAbove() );

    updateUpDown();
    emit configChanged();
}


void DlgDirectories::setData()
{
    DirList::Iterator it;

    if (ksettings.defaultDirList.count() > 0) {
        // we need to insert items in the reverse order
        // because "new TQListViewItem" puts itself at the beginning
        for (it = ksettings.defaultDirList.fromLast(); it != ksettings.defaultDirList.begin(); it--) {
            new TQListViewItem(lv_entries, (*it).extRegexp, (*it).defaultDir);
        }
        new TQListViewItem(lv_entries, (*it).extRegexp, (*it).defaultDir);
    }
}


void DlgDirectories::applyData()
{
    ksettings.defaultDirList.clear();
    TQListViewItemIterator it(lv_entries);

    for (; it.current(); ++it) {
        TQListViewItem *item = it.current();

        DirItem ditem;

        ditem.extRegexp = item->text(0);
        ditem.defaultDir = item->text(1);
        ksettings.defaultDirList.append(ditem);
    }
}

#include "dlgDirectories.moc"