kivio

kiviostencilsetinstaller.cpp

00001 /*
00002  * Kivio - Visual Modelling and Flowcharting
00003  * Copyright (C) 2004 Peter Simonsson
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 /* Based heavily on code from the icon theme installer
00021  * Copyright (C) 2000 Antonio Larrosa <larrosa@kde.org>
00022  */
00023 
00024 #include "kiviostencilsetinstaller.h"
00025 
00026 #include <qframe.h>
00027 #include <qlayout.h>
00028 #include <qcheckbox.h>
00029 #include <qstringlist.h>
00030 
00031 #include <klocale.h>
00032 #include <kurlrequester.h>
00033 #include <kmessagebox.h>
00034 #include <kio/netaccess.h>
00035 #include <kurl.h>
00036 #include <kstandarddirs.h>
00037 #include <ktar.h>
00038 #include <karchive.h>
00039 #include <kguiitem.h>
00040 #include <klineedit.h>
00041 
00042 namespace Kivio {
00043   StencilSetInstaller::StencilSetInstaller(QWidget *parent, const char *name)
00044     : KDialogBase(KDialogBase::Plain, i18n("Install Stencil Set"), Ok|Cancel, Ok, parent, name)
00045   {
00046     QFrame* page = plainPage();
00047     QVBoxLayout* l = new QVBoxLayout(page);
00048     l->setAutoAdd(true);
00049 
00050     m_url = new KURLRequester(page);
00051 
00052     setButtonOK(KGuiItem(i18n("&Install"), "button_ok"));
00053     resize(400, 10);
00054     connect( m_url->lineEdit(), SIGNAL( textChanged ( const QString & ) ), this, SLOT( slotUrlChanged( const QString & ) ) );
00055     slotUrlChanged( m_url->lineEdit()->text() );
00056     m_url->setFocus();
00057   }
00058 
00059   StencilSetInstaller::~StencilSetInstaller()
00060   {
00061   }
00062 
00063     void StencilSetInstaller::slotUrlChanged( const QString &text )
00064     {
00065         enableButtonOK( !text.isEmpty() );
00066     }
00067 
00068   void StencilSetInstaller::install(const QString& urlString)
00069   {
00070     KURL url(urlString);
00071 
00072     if(url.isEmpty()) return;
00073 
00074     QString tmpFile;
00075 
00076     if(!KIO::NetAccess::download(url, tmpFile, this)) {
00077       KMessageBox::error(this, i18n("Could not find the stencil set archive %1.").arg(url.prettyURL()));
00078       return;
00079     }
00080 
00081     KTar archive(urlString);
00082     archive.open(IO_ReadOnly);
00083     const KArchiveDirectory* rootDir = archive.directory();
00084 
00085     QStringList dirs = checkDirs(rootDir);
00086 
00087     if(dirs.isEmpty()) {
00088       KMessageBox::error(this, i18n("The file is not a valid stencil set archive."));
00089       archive.close();
00090       KIO::NetAccess::removeTempFile(tmpFile);
00091       return;
00092     }
00093 
00094     if(installStencilSets(rootDir, dirs)) {
00095       KMessageBox::information(this, i18n("The stencil set archive installed without errors."));
00096     } else {
00097       KMessageBox::error(this, i18n("The entire archive could not be installed successfully."));
00098     }
00099 
00100     archive.close();
00101     KIO::NetAccess::removeTempFile(tmpFile);
00102   }
00103 
00104   QStringList StencilSetInstaller::checkDirs(const KArchiveDirectory* rootDir)
00105   {
00106     QStringList dirs;
00107 
00108     const KArchiveEntry* possibleDir = 0;
00109     const KArchiveDirectory* subDir = 0;
00110 
00111     QStringList entries = rootDir->entries();
00112 
00113     for (QStringList::Iterator it = entries.begin(); it != entries.end(); ++it) {
00114       possibleDir = rootDir->entry(*it);
00115 
00116       if (possibleDir->isDirectory()) {
00117         subDir = dynamic_cast<const KArchiveDirectory*>( possibleDir );
00118 
00119         if (subDir && subDir->entry("desc")) {
00120           dirs.append(subDir->name());
00121         }
00122       }
00123     }
00124 
00125     return dirs;
00126   }
00127 
00128   bool StencilSetInstaller::installStencilSets(const KArchiveDirectory* rootDir, const QStringList& dirs)
00129   {
00130     QString installDir = locateLocal("data", "kivio/stencils");
00131     KStandardDirs::makeDir(installDir);
00132     const KArchiveDirectory* currentDir = 0;
00133     bool ok = true;
00134 
00135     for (QStringList::ConstIterator it = dirs.begin(); it != dirs.end(); ++it) {
00136       currentDir = dynamic_cast<const KArchiveDirectory*>(rootDir->entry(*it));
00137 
00138       if(!currentDir) {
00139         ok = false;
00140         continue;
00141       }
00142 
00143       currentDir->copyTo(installDir + "/" + currentDir->name());
00144     }
00145 
00146     return ok;
00147   }
00148 
00149   void StencilSetInstaller::slotOk()
00150   {
00151     install(m_url->url());
00152     accept();
00153   }
00154 }
00155 
00156 #include "kiviostencilsetinstaller.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys