kivio

kiviostencilsetinstaller.cpp
1/*
2 * Kivio - Visual Modelling and Flowcharting
3 * Copyright (C) 2004 Peter Simonsson
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20/* Based heavily on code from the icon theme installer
21 * Copyright (C) 2000 Antonio Larrosa <larrosa@kde.org>
22 */
23
24#include "kiviostencilsetinstaller.h"
25
26#include <tqframe.h>
27#include <tqlayout.h>
28#include <tqcheckbox.h>
29#include <tqstringlist.h>
30
31#include <tdelocale.h>
32#include <kurlrequester.h>
33#include <tdemessagebox.h>
34#include <tdeio/netaccess.h>
35#include <kurl.h>
36#include <kstandarddirs.h>
37#include <ktar.h>
38#include <karchive.h>
39#include <kguiitem.h>
40#include <klineedit.h>
41
42namespace Kivio {
43 StencilSetInstaller::StencilSetInstaller(TQWidget *parent, const char *name)
44 : KDialogBase(KDialogBase::Plain, i18n("Install Stencil Set"), Ok|Cancel, Ok, parent, name)
45 {
46 TQFrame* page = plainPage();
47 TQVBoxLayout* l = new TQVBoxLayout(page);
48 l->setAutoAdd(true);
49
50 m_url = new KURLRequester(page);
51
52 setButtonOK(KGuiItem(i18n("&Install"), "button_ok"));
53 resize(400, 10);
54 connect( m_url->lineEdit(), TQ_SIGNAL( textChanged ( const TQString & ) ), this, TQ_SLOT( slotUrlChanged( const TQString & ) ) );
55 slotUrlChanged( m_url->lineEdit()->text() );
56 m_url->setFocus();
57 }
58
59 StencilSetInstaller::~StencilSetInstaller()
60 {
61 }
62
63 void StencilSetInstaller::slotUrlChanged( const TQString &text )
64 {
65 enableButtonOK( !text.isEmpty() );
66 }
67
68 void StencilSetInstaller::install(const TQString& urlString)
69 {
70 KURL url(urlString);
71
72 if(url.isEmpty()) return;
73
74 TQString tmpFile;
75
76 if(!TDEIO::NetAccess::download(url, tmpFile, this)) {
77 KMessageBox::error(this, i18n("Could not find the stencil set archive %1.").arg(url.prettyURL()));
78 return;
79 }
80
81 KTar archive(urlString);
82 archive.open(IO_ReadOnly);
83 const KArchiveDirectory* rootDir = archive.directory();
84
85 TQStringList dirs = checkDirs(rootDir);
86
87 if(dirs.isEmpty()) {
88 KMessageBox::error(this, i18n("The file is not a valid stencil set archive."));
89 archive.close();
90 TDEIO::NetAccess::removeTempFile(tmpFile);
91 return;
92 }
93
94 if(installStencilSets(rootDir, dirs)) {
95 KMessageBox::information(this, i18n("The stencil set archive installed without errors."));
96 } else {
97 KMessageBox::error(this, i18n("The entire archive could not be installed successfully."));
98 }
99
100 archive.close();
101 TDEIO::NetAccess::removeTempFile(tmpFile);
102 }
103
104 TQStringList StencilSetInstaller::checkDirs(const KArchiveDirectory* rootDir)
105 {
106 TQStringList dirs;
107
108 const KArchiveEntry* possibleDir = 0;
109 const KArchiveDirectory* subDir = 0;
110
111 TQStringList entries = rootDir->entries();
112
113 for (TQStringList::Iterator it = entries.begin(); it != entries.end(); ++it) {
114 possibleDir = rootDir->entry(*it);
115
116 if (possibleDir->isDirectory()) {
117 subDir = dynamic_cast<const KArchiveDirectory*>( possibleDir );
118
119 if (subDir && subDir->entry("desc")) {
120 dirs.append(subDir->name());
121 }
122 }
123 }
124
125 return dirs;
126 }
127
128 bool StencilSetInstaller::installStencilSets(const KArchiveDirectory* rootDir, const TQStringList& dirs)
129 {
130 TQString installDir = locateLocal("data", "kivio/stencils");
131 TDEStandardDirs::makeDir(installDir);
132 const KArchiveDirectory* currentDir = 0;
133 bool ok = true;
134
135 for (TQStringList::ConstIterator it = dirs.begin(); it != dirs.end(); ++it) {
136 currentDir = dynamic_cast<const KArchiveDirectory*>(rootDir->entry(*it));
137
138 if(!currentDir) {
139 ok = false;
140 continue;
141 }
142
143 currentDir->copyTo(installDir + "/" + currentDir->name());
144 }
145
146 return ok;
147 }
148
149 void StencilSetInstaller::slotOk()
150 {
151 install(m_url->url());
152 accept();
153 }
154}
155
156#include "kiviostencilsetinstaller.moc"