summaryrefslogtreecommitdiffstats
path: root/digikam/libs/dialogs/deletedialog.cpp
blob: 9816e4fef34c51307ed5f68853d7900576247bfb (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
/* ============================================================
 *
 * This file is a part of digiKam project
 * http://www.digikam.org
 *
 * Date        : 2005-05-07
 * Description : a dialog to delete item.
 * 
 * Copyright (C) 2004 by Michael Pyne <michael.pyne@kdemail.net>
 * Copyright (C) 2006 by Ian Monroe <ian@monroe.nu>
 * Copyright (C) 2006-2007 by Marcel Wiesweg <marcel.wiesweg@gmx.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, 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.
 * 
 * ============================================================ */

// TQt includes.

#include <tqstringlist.h>
#include <tqlayout.h>
#include <tqlabel.h>
#include <tqpushbutton.h>
#include <tqtimer.h>
#include <tqvbox.h>
#include <tqhbox.h>
#include <tqwidgetstack.h>

// KDE includes.

#include <kconfig.h>
#include <kdeversion.h>
#include <kdialogbase.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <kio/job.h>
#include <klocale.h>
#include <kstdguiitem.h>

// Local includes.

#include "deletedialog.h"
#include "albumsettings.h"
#include "deletedialog.moc"

namespace Digikam
{

//////////////////////////////////////////////////////////////////////////////
// DeleteWidget implementation
//////////////////////////////////////////////////////////////////////////////

DeleteWidget::DeleteWidget(TQWidget *parent, const char *name)
            : DeleteDialogBase(parent, name),
              m_listMode(DeleteDialogMode::Files),
              m_deleteMode(DeleteDialogMode::UseTrash)
{
    ddCheckBoxStack->raiseWidget(ddShouldDeletePage);

    bool deleteInstead = !AlbumSettings::instance()->getUseTrash();
    slotShouldDelete(deleteInstead);
    ddShouldDelete->setChecked(deleteInstead);
}

void DeleteWidget::setFiles(const KURL::List &files)
{
    ddFileList->clear();
    for( KURL::List::ConstIterator it = files.begin(); it != files.end(); it++)
    {
        if( (*it).isLocalFile() ) //path is nil for non-local
            ddFileList->insertItem( (*it).path() );
        else if ( (*it).protocol() == "digikamalbums")
            ddFileList->insertItem( (*it).path() );
        else
            ddFileList->insertItem( (*it).prettyURL() );
    }
    updateText();
}

void DeleteWidget::slotShouldDelete(bool shouldDelete)
{
    setDeleteMode(shouldDelete ? DeleteDialogMode::DeletePermanently : DeleteDialogMode::UseTrash);
}

void DeleteWidget::setDeleteMode(DeleteDialogMode::DeleteMode deleteMode)
{
    m_deleteMode = deleteMode;
    updateText();
}

void DeleteWidget::setListMode(DeleteDialogMode::ListMode listMode)
{
    m_listMode = listMode;
    updateText();
}

void DeleteWidget::updateText()
{
    switch (m_listMode)
    {
        case DeleteDialogMode::Files:

        // Delete files

        if (m_deleteMode == DeleteDialogMode::DeletePermanently)
        {
            ddDeleteText->setText(i18n("<qt>These items will be <b>permanently "
                                       "deleted</b> from your hard disk.</qt>"));
            ddWarningIcon->setPixmap(KGlobal::iconLoader()->loadIcon("messagebox_warning",
                KIcon::Desktop, KIcon::SizeLarge));
        }
        else
        {
            ddDeleteText->setText(i18n("<qt>These items will be moved to Trash.</qt>"));
            ddWarningIcon->setPixmap(KGlobal::iconLoader()->loadIcon("trashcan_full",
                KIcon::Desktop, KIcon::SizeLarge));
        }
        ddNumFiles->setText(i18n("<b>1</b> file selected.", "<b>%n</b> files selected.", ddFileList->count()));
        break;

        case DeleteDialogMode::Albums:

        // Delete albums = folders

        if (m_deleteMode == DeleteDialogMode::DeletePermanently)
        {
            ddDeleteText->setText(i18n("<qt>These albums will be <b>permanently "
                                       "deleted</b> from your hard disk.</qt>"));
            ddWarningIcon->setPixmap(KGlobal::iconLoader()->loadIcon("messagebox_warning",
                                     KIcon::Desktop, KIcon::SizeLarge));
        }
        else
        {
            ddDeleteText->setText(i18n("<qt>These albums will be moved to Trash.</qt>"));
            ddWarningIcon->setPixmap(KGlobal::iconLoader()->loadIcon("trashcan_full",
                                     KIcon::Desktop, KIcon::SizeLarge));
        }
        ddNumFiles->setText(i18n("<b>1</b> album selected.", "<b>%n</b> albums selected.", ddFileList->count()));
        break;

        case DeleteDialogMode::Subalbums:

        // As above, but display additional warning

        if (m_deleteMode == DeleteDialogMode::DeletePermanently)
        {
            ddDeleteText->setText(i18n("<qt>These albums will be <b>permanently "
                                       "deleted</b> from your hard disk.<br>"
                                       "Note that <b>all subalbums</b> "
                                       "are included in this list and will "
                                       "be deleted permanently as well.</qt>"));
            ddWarningIcon->setPixmap(KGlobal::iconLoader()->loadIcon("messagebox_warning",
                                     KIcon::Desktop, KIcon::SizeLarge));
        }
        else
        {
            ddDeleteText->setText(i18n("<qt>These albums will be moved to Trash.<br>"
                                       "Note that <b>all subalbums</b> "
                                       "are included in this list and will "
                                       "be moved to Trash as well.</qt>"));
            ddWarningIcon->setPixmap(KGlobal::iconLoader()->loadIcon("trashcan_full",
                                     KIcon::Desktop, KIcon::SizeLarge));
        }
        ddNumFiles->setText(i18n("<b>1</b> album selected.", "<b>%n</b> albums selected.", ddFileList->count()));
        break;

    }
}

//////////////////////////////////////////////////////////////////////////////
// DeleteDialog implementation
//////////////////////////////////////////////////////////////////////////////

DeleteDialog::DeleteDialog(TQWidget *parent, const char *name) 
            : KDialogBase(Swallow, WStyle_DialogBorder, parent, name,
                          true, // modal
                          i18n("About to delete selected files"), // caption
                          Ok | Cancel, // available buttons
                          Ok,  // default button
                          true // use separator between buttons and the main widget
                         ),
              m_saveShouldDeleteUserPreference(true),
              m_saveDoNotShowAgain(false),
              m_trashGuiItem(i18n("&Move to Trash"), "trashcan_full")
{
    m_widget = new DeleteWidget(this, "delete_dialog_widget");
    setMainWidget(m_widget);

    m_widget->setMinimumSize(400, 300);
    setMinimumSize(410, 326);
    adjustSize();

    slotShouldDelete(shouldDelete());
    connect(m_widget->ddShouldDelete, TQT_SIGNAL(toggled(bool)),
            this, TQT_SLOT(slotShouldDelete(bool)));

    actionButton(Ok)->setFocus();
}

bool DeleteDialog::confirmDeleteList(const KURL::List& condemnedFiles,
                                     DeleteDialogMode::ListMode listMode,
                                     DeleteDialogMode::DeleteMode deleteMode)
{
    setURLs(condemnedFiles);
    presetDeleteMode(deleteMode);
    setListMode(listMode);

    if (deleteMode == DeleteDialogMode::NoChoiceTrash)
    {
        if (!AlbumSettings::instance()->getShowTrashDeleteDialog())
            return true;
    }
    return exec() == TQDialog::Accepted;
}

void DeleteDialog::setURLs(const KURL::List &files)
{
    m_widget->setFiles(files);
}

void DeleteDialog::accept()
{
    // Save user's preference
    AlbumSettings *settings = AlbumSettings::instance();

    if (m_saveShouldDeleteUserPreference)
    {
        settings->setUseTrash(!shouldDelete());
    }
    if (m_saveDoNotShowAgain)
    {
        settings->setShowTrashDeleteDialog(!m_widget->ddDoNotShowAgain->isChecked());
    }

    settings->saveSettings();

    KDialogBase::accept();
}

void DeleteDialog::slotShouldDelete(bool shouldDelete)
{
    // This is called once from constructor, and then when the user changed the checkbox state.
    // In that case, save the user's preference.
    m_saveShouldDeleteUserPreference = true;
    setButtonGuiItem(Ok, shouldDelete ? KStdGuiItem::del() : m_trashGuiItem);
}

void DeleteDialog::presetDeleteMode(DeleteDialogMode::DeleteMode mode)
{
    switch (mode)
    {
        case DeleteDialogMode::NoChoiceTrash:
        {
            // access the widget directly, signals will be fired to DeleteDialog and DeleteWidget
            m_widget->ddShouldDelete->setChecked(false);
            m_widget->ddCheckBoxStack->raiseWidget(m_widget->ddDoNotShowAgainPage);
            m_saveDoNotShowAgain = true;
            break;
        }
        case DeleteDialogMode::NoChoiceDeletePermanently:
        {
            m_widget->ddShouldDelete->setChecked(true);
            m_widget->ddCheckBoxStack->hide();
            break;
        }
        case DeleteDialogMode::UserPreference:
        {
            break;
        }
        case DeleteDialogMode::UseTrash:
        case DeleteDialogMode::DeletePermanently:
        {
            // toggles signals which do the rest
            m_widget->ddShouldDelete->setChecked(mode == DeleteDialogMode::DeletePermanently);

            // the preference set by this preset method will be ignored
            // for the next DeleteDialog instance and not stored as user preference.
            // Only if the user once changes this value, it will be taken as user preference.
            m_saveShouldDeleteUserPreference = false;
            break;
        }
    }
}

void DeleteDialog::setListMode(DeleteDialogMode::ListMode mode)
{
    m_widget->setListMode(mode);
    switch (mode)
    {
        case DeleteDialogMode::Files:
            setCaption(i18n("About to delete selected files"));
            break;

        case DeleteDialogMode::Albums:
        case DeleteDialogMode::Subalbums:
            setCaption(i18n("About to delete selected albums"));
            break;
    }
}

} // namespace Digikam