showarchivestructuredialog.cpp 7.03 KB
Newer Older
1
/*
2
   Copyright (C) 2013-2017 Montel Laurent <montel@kde.org>
3

Laurent Montel's avatar
Laurent Montel committed
4
5
6
7
   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.
8

Laurent Montel's avatar
Laurent Montel committed
9
10
11
12
   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.
13

Laurent Montel's avatar
Laurent Montel committed
14
15
16
17
   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
18
19
20
*/

#include "showarchivestructuredialog.h"
21
#include "core/utils.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include "PimCommon/PimUtil"
Laurent Montel's avatar
Laurent Montel committed
23
#include <QDialog>
24
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
25
#include <KZip>
Laurent Montel's avatar
Laurent Montel committed
26
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
27
28

#include <QTreeWidget>
Laurent Montel's avatar
Laurent Montel committed
29
#include <QHeaderView>
30
#include <KSharedConfig>
Laurent Montel's avatar
Laurent Montel committed
31
32
33
34
#include <QDialogButtonBox>
#include <KConfigGroup>
#include <QPushButton>
#include <QVBoxLayout>
35

Laurent Montel's avatar
Laurent Montel committed
36
37
#include <ktreewidgetsearchline.h>

38
ShowArchiveStructureDialog::ShowArchiveStructureDialog(const QString &filename, QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
39
    : QDialog(parent),
40
      mFileName(filename)
41
{
Laurent Montel's avatar
Laurent Montel committed
42
43
    setWindowTitle(i18n("Show Archive Content on file \"%1\"", filename));
    setModal(true);
Laurent Montel's avatar
Laurent Montel committed
44
45
46
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    mTreeWidget = new QTreeWidget(this);
Laurent Montel's avatar
Laurent Montel committed
47
    mTreeWidget->header()->hide();
48
    mTreeWidget->setAlternatingRowColors(true);
Laurent Montel's avatar
Laurent Montel committed
49

Laurent Montel's avatar
Laurent Montel committed
50
    KTreeWidgetSearchLine *searchLine = new KTreeWidgetSearchLine(this, mTreeWidget);
Laurent Montel's avatar
Laurent Montel committed
51
    searchLine->setPlaceholderText(i18n("Search..."));
Laurent Montel's avatar
Laurent Montel committed
52
53
54
55
56
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, this);
    QPushButton *user1Button = new QPushButton;
    buttonBox->addButton(user1Button, QDialogButtonBox::ActionRole);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &ShowArchiveStructureDialog::reject);

Laurent Montel's avatar
Laurent Montel committed
57
    mainLayout->addWidget(searchLine);
Laurent Montel's avatar
Laurent Montel committed
58
59
    mainLayout->addWidget(mTreeWidget);
    mainLayout->addWidget(buttonBox);
Yuri Chornoivan's avatar
Yuri Chornoivan committed
60
    user1Button->setText(i18n("Save As Text..."));
Laurent Montel's avatar
Laurent Montel committed
61
62
63
64
65
    const bool result = fillTree();
    if (result) {
        mTreeWidget->expandAll();
        connect(user1Button, &QPushButton::clicked, this, &ShowArchiveStructureDialog::slotExportAsLogFile);
    }
Laurent Montel's avatar
Laurent Montel committed
66
    user1Button->setEnabled(result);
Laurent Montel's avatar
Laurent Montel committed
67
    readConfig();
68
69
70
71
}

ShowArchiveStructureDialog::~ShowArchiveStructureDialog()
{
72
    writeConfig();
Laurent Montel's avatar
Laurent Montel committed
73
}
74

Laurent Montel's avatar
Laurent Montel committed
75
76
void ShowArchiveStructureDialog::slotExportAsLogFile()
{
77
    PimCommon::Util::saveTextAs(mLogFile, QStringLiteral("%1 (*.txt)").arg(i18nc("qfiledialog filter files text", "Text Files")), this, QUrl(),
78
                                i18nc("@title:window", "Export Log File"));
Laurent Montel's avatar
Laurent Montel committed
79
80
}

81
bool ShowArchiveStructureDialog::fillTree()
Laurent Montel's avatar
Laurent Montel committed
82
{
83
    KZip *zip = new KZip(mFileName);
Laurent Montel's avatar
Laurent Montel committed
84
85
    bool result = zip->open(QIODevice::ReadOnly);
    if (!result) {
Yuri Chornoivan's avatar
Yuri Chornoivan committed
86
        KMessageBox::error(this, i18n("Archive cannot be opened in read mode."), i18n("Cannot open archive"));
Laurent Montel's avatar
Laurent Montel committed
87
        delete zip;
Laurent Montel's avatar
Laurent Montel committed
88
        return false;
Laurent Montel's avatar
Laurent Montel committed
89
90
    }
    const KArchiveDirectory *topDirectory = zip->directory();
91
92
93
    const bool isAValidArchive = searchArchiveElement(Utils::infoPath(), topDirectory, i18n("Info"));
    if (!isAValidArchive) {
        KMessageBox::error(this, i18n("This is not kdepim archive."), i18n("Show information"));
94
        result = false;
95
96
97
98
99
100
101
102
103
104
105
106
    } else {
        searchArchiveElement(Utils::mailsPath(), topDirectory, Utils::appTypeToI18n(Utils::KMail));
        searchArchiveElement(Utils::alarmPath(), topDirectory, Utils::appTypeToI18n(Utils::KAlarm));
        searchArchiveElement(Utils::calendarPath(), topDirectory, Utils::appTypeToI18n(Utils::KOrganizer));
        searchArchiveElement(Utils::addressbookPath(), topDirectory, Utils::appTypeToI18n(Utils::KAddressBook));
        searchArchiveElement(Utils::identitiesPath(), topDirectory, Utils::storedTypeToI18n(Utils::Identity));
        searchArchiveElement(Utils::resourcesPath(), topDirectory, Utils::storedTypeToI18n(Utils::Resources));
        searchArchiveElement(Utils::configsPath(), topDirectory, Utils::storedTypeToI18n(Utils::Config));
        searchArchiveElement(Utils::transportsPath(), topDirectory, Utils::storedTypeToI18n(Utils::MailTransport));
        searchArchiveElement(Utils::dataPath(), topDirectory, Utils::storedTypeToI18n(Utils::Data));
        searchArchiveElement(Utils::akonadiPath(), topDirectory, Utils::storedTypeToI18n(Utils::AkonadiDb));
    }
Laurent Montel's avatar
Laurent Montel committed
107
    delete zip;
108
    return result;
Laurent Montel's avatar
Laurent Montel committed
109
110
}

111
bool ShowArchiveStructureDialog::searchArchiveElement(const QString &path, const KArchiveDirectory *topDirectory, const QString &name)
Laurent Montel's avatar
Laurent Montel committed
112
113
{
    const KArchiveEntry *topEntry = topDirectory->entry(path);
114
    bool result = true;
Laurent Montel's avatar
Laurent Montel committed
115
    if (topEntry) {
Laurent Montel's avatar
Laurent Montel committed
116
        mLogFile += name + QLatin1Char('\n');
Laurent Montel's avatar
Laurent Montel committed
117
        QTreeWidgetItem *item = addTopItem(name);
Laurent Montel's avatar
Laurent Montel committed
118
        addSubItems(item, topEntry, 0);
119
120
    } else {
        result = false;
Laurent Montel's avatar
Laurent Montel committed
121
    }
122
    return result;
Laurent Montel's avatar
Laurent Montel committed
123
}
Laurent Montel's avatar
Laurent Montel committed
124

Laurent Montel's avatar
Laurent Montel committed
125
void ShowArchiveStructureDialog::addSubItems(QTreeWidgetItem *parent, const KArchiveEntry *entry, int indent)
Laurent Montel's avatar
Laurent Montel committed
126
127
{
    const KArchiveDirectory *dir = static_cast<const KArchiveDirectory *>(entry);
Laurent Montel's avatar
Laurent Montel committed
128
    ++indent;
Laurent Montel's avatar
Laurent Montel committed
129
    const QString space = QString(indent * 2, QLatin1Char(' '));
Laurent Montel's avatar
Laurent Montel committed
130
131
    const QStringList lst = dir->entries();
    for (const QString &entryName : lst) {
Laurent Montel's avatar
Laurent Montel committed
132
133
134
135
136
        const KArchiveEntry *entry = dir->entry(entryName);
        if (entry) {
            if (entry->isDirectory()) {
                const KArchiveDirectory *dirEntry = static_cast<const KArchiveDirectory *>(entry);
                QTreeWidgetItem *newTopItem = addItem(parent, dirEntry->name());
137
                QFont font(newTopItem->font(0));
Laurent Montel's avatar
Laurent Montel committed
138
                font.setBold(true);
Laurent Montel's avatar
Laurent Montel committed
139
                mLogFile += space + dirEntry->name() + QLatin1Char('\n');
140
                newTopItem->setFont(0, font);
Laurent Montel's avatar
Laurent Montel committed
141
                addSubItems(newTopItem, entry, indent);
Laurent Montel's avatar
Laurent Montel committed
142
143
            } else if (entry->isFile()) {
                const KArchiveFile *file = static_cast<const KArchiveFile *>(entry);
Laurent Montel's avatar
Laurent Montel committed
144
                addItem(parent, file->name());
Laurent Montel's avatar
Laurent Montel committed
145
                mLogFile += space + file->name() + QLatin1Char('\n');
Laurent Montel's avatar
Laurent Montel committed
146
147
148
            }
        }
    }
Laurent Montel's avatar
Laurent Montel committed
149
150
}

Laurent Montel's avatar
Laurent Montel committed
151
QTreeWidgetItem *ShowArchiveStructureDialog::addItem(QTreeWidgetItem *parent, const QString &name)
Laurent Montel's avatar
Laurent Montel committed
152
153
{
    QTreeWidgetItem *item = new QTreeWidgetItem(parent);
Laurent Montel's avatar
Laurent Montel committed
154
    item->setText(0, name);
Laurent Montel's avatar
Laurent Montel committed
155
156
157
158
    return item;
}

QTreeWidgetItem *ShowArchiveStructureDialog::addTopItem(const QString &name)
Laurent Montel's avatar
Laurent Montel committed
159
160
{
    QTreeWidgetItem *item = new QTreeWidgetItem;
Laurent Montel's avatar
Laurent Montel committed
161
162
    QFont font = item->font(0);
    font.setBold(true);
Laurent Montel's avatar
Laurent Montel committed
163
164
    item->setFont(0, font);
    item->setText(0, name);
Laurent Montel's avatar
Laurent Montel committed
165
166
    mTreeWidget->addTopLevelItem(item);
    return item;
167
168
}

169
170
void ShowArchiveStructureDialog::writeConfig()
{
Laurent Montel's avatar
Laurent Montel committed
171
172
    KConfigGroup group(KSharedConfig::openConfig(), "ShowArchiveStructureDialog");
    group.writeEntry("Size", size());
173
174
175
176
}

void ShowArchiveStructureDialog::readConfig()
{
Laurent Montel's avatar
Laurent Montel committed
177
178
179
180
    KConfigGroup group(KSharedConfig::openConfig(), "ShowArchiveStructureDialog");
    const QSize sizeDialog = group.readEntry("Size", QSize(600, 400));
    if (sizeDialog.isValid()) {
        resize(sizeDialog);
181
182
183
    }
}