add more spacing
[personal-kdebase.git] / workspace / kcontrol / kfontinst / apps / Installer.cpp
blob7878fdf975be1df2b2b2a362366526d302bd9d37
1 /*
2 * KFontInst - KDE Font Installer
4 * Copyright 2003-2007 Craig Drummond <craig@kde.org>
6 * ----
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; see the file COPYING. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
24 #include "Installer.h"
25 #include "Misc.h"
26 #include <QtCore/QFile>
27 #include <KDE/KCmdLineArgs>
28 #include <KDE/KAboutData>
29 #include <KDE/KApplication>
30 #include <KDE/KZip>
31 #include <KDE/KTempDir>
32 #include <KDE/KMessageBox>
33 #include <KDE/KStandardDirs>
34 #include <KDE/KIO/NetAccess>
35 #include "JobRunner.h"
36 #include "CreateParent.h"
38 namespace KFI
41 int CInstaller::install(const QSet<KUrl> &urls)
43 QSet<KUrl>::ConstIterator it(urls.begin()),
44 end(urls.end());
45 bool sysInstall(false);
46 CJobRunner *jobRunner=new CJobRunner(itsParent);
48 if(!Misc::root())
50 switch(KMessageBox::questionYesNoCancel(itsParent,
51 i18n("Do you wish to install the font(s) for personal use "
52 "(only available to you), or "
53 "system-wide (available to all users)?"),
54 i18n("Where to Install"), KGuiItem(i18n(KFI_KIO_FONTS_USER)),
55 KGuiItem(i18n(KFI_KIO_FONTS_SYS))))
57 case KMessageBox::No:
58 if(!jobRunner->getAdminPasswd(itsParent))
59 return -1;
60 break;
61 case KMessageBox::Cancel:
62 return -1;
63 default:
64 break;
68 QSet<KUrl> instUrls;
70 for(; it!=end; ++it)
72 KUrl local(KIO::NetAccess::mostLocalUrl(*it, NULL));
73 bool package(false);
75 if(local.isLocalFile())
77 QString localFile(local.path());
79 if(Misc::isPackage(localFile))
81 KZip zip(localFile);
83 package=true;
84 if(zip.open(QIODevice::ReadOnly))
86 const KArchiveDirectory *zipDir=zip.directory();
88 if(zipDir)
90 QStringList fonts(zipDir->entries());
92 if(fonts.count())
94 QStringList::ConstIterator it(fonts.begin()),
95 end(fonts.end());
97 if(!itsTempDir)
99 itsTempDir=new KTempDir(KStandardDirs::locateLocal("tmp", KFI_TMP_DIR_PREFIX));
100 itsTempDir->setAutoRemove(true);
103 for(; it!=end; ++it)
105 const KArchiveEntry *entry=zipDir->entry(*it);
107 if(entry && entry->isFile())
109 ((KArchiveFile *)entry)->copyTo(itsTempDir->name());
111 QString name(entry->name());
114 // Cant install hidden fonts, therefore need to unhide 1st!
115 if(Misc::isHidden(name))
117 ::rename(QFile::encodeName(itsTempDir->name()+name).data(),
118 QFile::encodeName(itsTempDir->name()+name.mid(1)).data());
119 name=name.mid(1);
121 instUrls.insert(KUrl(itsTempDir->name()+name));
129 if(!package)
131 KUrl::List associatedUrls;
133 CJobRunner::getAssociatedUrls(*it, associatedUrls, false, itsParent);
134 instUrls.insert(*it);
136 KUrl::List::Iterator aIt(associatedUrls.begin()),
137 aEnd(associatedUrls.end());
139 for(; aIt!=aEnd; ++aIt)
140 instUrls.insert(*aIt);
144 if(instUrls.count())
146 CJobRunner::ItemList list;
147 QSet<KUrl>::ConstIterator it(instUrls.begin()),
148 end(instUrls.end());
150 for(; it!=end; ++it)
151 list.append(*it);
153 return jobRunner->exec(CJobRunner::CMD_INSTALL, list,
154 KUrl(Misc::root()
155 ? KFI_KIO_FONTS_PROTOCOL":/"
156 : sysInstall
157 ? KFI_KIO_FONTS_PROTOCOL":/"KFI_KIO_FONTS_SYS"/"
158 : KFI_KIO_FONTS_PROTOCOL":/"KFI_KIO_FONTS_USER"/"));
160 else
161 return -1;
164 CInstaller::~CInstaller()
166 delete itsTempDir;
171 static KAboutData aboutData("kfontinst", 0, ki18n("Font Installer"), "1.0", ki18n("Simple font installer"),
172 KAboutData::License_GPL, ki18n("(C) Craig Drummond, 2007"));
174 int main(int argc, char **argv)
177 // Do a quick check to see if we're just being run to configure X...
178 if(3==argc && 0==strcmp(argv[1], "-x"))
180 QString dir(KFI::Misc::dirSyntax(QFile::decodeName(argv[2])));
182 return KFI::Misc::dExists(dir) && KFI::Misc::configureForX11(dir) ? 0 : -1;
184 else
186 KCmdLineArgs::init(argc, argv, &aboutData);
188 KCmdLineOptions options;
189 options.add("x <folder>", ki18n("Configure folder for X11 - create fonts.dir and fonts.scale, plus remove hidden entries."
190 " (NOTE: Use this option on its own.)"));
191 options.add("embed <winid>", ki18n("Makes the dialog transient for an X app specified by winid"));
192 options.add("+[URL]", ki18n("URL to install"));
193 KCmdLineArgs::addCmdLineOptions(options);
195 QSet<KUrl> urls;
196 KCmdLineArgs *args(KCmdLineArgs::parsedArgs());
198 for(int i=0; i < args->count(); i++)
199 urls.insert(args->url(i));
201 if(urls.count())
203 KLocale::setMainCatalog(KFI_CATALOGUE);
205 KApplication app;
206 QString opt(args->getOption("embed"));
207 KFI::CInstaller inst(createParent(opt.size() ? opt.toInt(0, 16) : 0));
209 return inst.install(urls);
212 return -1;