• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

kapptemplate

  • sources
  • kde-4.12
  • kdesdk
  • kapptemplate
generatepage.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright 2001 Bernd Gehrmann <bernd@kdevelop.org> *
3  * Copyright 2004-2005 Sascha Cunz <sascha@kdevelop.org> *
4  * Copyright 2007 Alexander Dymo <adymo@kdevelop.org> *
5  * Copyright 2008 Anne-Marie Mahfouf <annma@kde.org> *
6  * *
7  * This program is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program; if not, write to the *
19  * Free Software Foundation, Inc., *
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
21  ***************************************************************************/
22 
23 #include <QDir>
24 #include <QFileInfo>
25 #include <QTextCodec>
26 
27 #include <KDebug>
28 #include <kio/copyjob.h>
29 #include <kmacroexpander.h>
30 #include <kmessagebox.h>
31 #include <kmimetype.h>
32 #include <kstandarddirs.h>
33 #include <ktempdir.h>
34 #include <ktar.h>
35 #include <kzip.h>
36 
37 #include "kapptemplate.h"
38 #include "generatepage.h"
39 #include "prefs.h"
40 
41 const QString current_year = QString().setNum(QDate::currentDate().year());
42 
43 GeneratePage::GeneratePage(QWidget *parent)
44  : QWizardPage(parent)
45 {
46  setTitle(i18n("Generating your project"));
47  ui_generate.setupUi(this);
48 }
49 
50 bool GeneratePage::unpackArchive(const KArchiveDirectory *dir, const QString &dest)
51 {
52  kDebug(9010) << "unpacking dir:" << dir->name() << "to" << dest;
53  QStringList entries = dir->entries();
54  kDebug(9010) << "entries:" << entries.join(",");
55 
56  KTempDir tdir;
57 
58  bool ret = true;
59 
60  //create path were we want copy files to
61  if(!QDir::root().mkpath(dest)){
62  KMessageBox::sorry(0, i18n("%1 cannot be created.", dest));
63  return false;
64  }
65 
66  int progress = 0;
67 
68  foreach (const QString &entry, entries)
69  {
70  progress++;
71  ui_generate.progressBar->setValue( (progress / entries.size()) * 100);
72  //don't copy .kdevtemplate
73  if (entry.endsWith(".kdevtemplate"))
74  continue;
75  if (entry == templateName+".png")
76  continue;
77  if (dir->entry(entry)->isDirectory()) {
78  const KArchiveDirectory *file = (KArchiveDirectory *)dir->entry(entry);
79  QString newdest = dest+"/"+file->name();
80  if( !QFileInfo( newdest ).exists() ) {
81  QDir::root().mkdir( newdest );
82  }
83  ret |= unpackArchive(file, newdest);
84  }
85  else if (dir->entry(entry)->isFile()) {
86  const KArchiveFile *file = (KArchiveFile *)dir->entry(entry);
87  file->copyTo(tdir.name());
88  QString destName = dest + '/' + file->name();
89  if (!destName.contains("/icons/")) {
90  if (!copyFile(QDir::cleanPath(tdir.name()+'/'+file->name()),
91  KMacroExpander::expandMacros(destName, m_variables))) {
92  KMessageBox::sorry(0, i18n("The file %1 cannot be created.", dest));
93  feedback.append(i18n("\n\nThe file %1 cannot be created.", dest));
94  ui_generate.label->setText(feedback);
95  return false;
96  }
97  }
98  else {
99  //do not parse .png but parse filemanes for placeholders
100  if (!QFile(QDir::cleanPath(tdir.name()+'/'+file->name())).copy(KMacroExpander::expandMacros(destName, m_variables))) {
101  KMessageBox::sorry(0, i18n("The file %1 cannot be created.", dest));
102  feedback.append(i18n("\n\nThe file %1 cannot be created.", dest));
103  ui_generate.label->setText(feedback);
104  return false;
105  }
106  kDebug() << "after copying.... " << endl;
107  }
108  }
109  }
110  tdir.unlink();
111  return ret;
112 }
113 
114 bool GeneratePage::copyFile(const QString &source, const QString &dest)
115 {
116  kDebug(9010) << "copy:" << source << "to" << dest;
117  QFile inputFile(source);
118  QFile outputFile(dest);
119 
120  QFileInfo temp(source);
121 
122  if (inputFile.open(QFile::ReadOnly) && outputFile.open(QFile::WriteOnly))
123  {
124  if( KMimeType::isBinaryData(source) ) {
125  KIO::CopyJob* job = KIO::copy( KUrl(source), KUrl(dest), KIO::Overwrite );
126  if( !job->exec() ) {
127  return false;
128  }
129  return true;
130  } else if(temp.suffix().compare("png",Qt::CaseInsensitive) == 0) {
131  QDataStream input(&inputFile);
132  QDataStream output(&outputFile);
133 
134  while(!input.atEnd()) {
135  qint8 t;
136  input >> t;
137  output << t;
138  }
139  } else {
140  QTextStream input(&inputFile);
141  input.setCodec(QTextCodec::codecForName("UTF-8"));
142  QTextStream output(&outputFile);
143  output.setCodec(QTextCodec::codecForName("UTF-8"));
144 
145  while(!input.atEnd()) {
146  QString line = input.readLine();
147 
148  output << KMacroExpander::expandMacros(line, m_variables) << "\n";
149  }
150  }
151 
152  struct stat fmode;
153  ::fstat(inputFile.handle(), &fmode);
154  ::fchmod(outputFile.handle(), fmode.st_mode);
155 
156  return true;
157  } else {
158  inputFile.close();
159  outputFile.close();
160  return false;
161  }
162 }
163 
164 void GeneratePage::initializePage()
165 {
166  feedback = i18n("Generation Progress\n");
167  ui_generate.label->setText(feedback);
168  templateName = field("tempName").toString();
169  if (templateName.isEmpty()) {
170  templateName = "kde4";
171  }
172  QString templateArchive = KGlobal::dirs()->findResource("data", QString("kdevappwizard/templates/%1.zip").arg(templateName));
173  if( templateArchive.isEmpty() ) {
174  templateArchive = KGlobal::dirs()->findResource("data", QString("kdevappwizard/templates/%1.tar.bz2").arg(templateName));
175  }
176  //create dir where template project will be copied
177  QString appName = field("appName").toString();
178  QString version = field("version").toString();
179  KUrl dest(field("url").toString()+"/"+appName.toLower());
180  m_variables.clear();
181  m_variables["CURRENT_YEAR"]=current_year;
182  m_variables["APPNAME"] = appName;
183  m_variables["APPNAMEUC"] = appName.toUpper();
184  m_variables["APPNAMELC"] = appName.toLower();
185  m_variables["PROJECTDIRNAME"] = appName.toLower();
186  m_variables["APPNAMEFU"] = appName.replace(0, 1, appName.toUpper().at(0));
187  m_variables["AUTHOR"] = field("author").toString();
188  m_variables["EMAIL"] = field("email").toString();
189  m_variables["VERSION"] = version;
190  m_variables["VERSIONCONTROLPLUGIN"] = version;
191  m_variables["PROJECTDIRNAME"] = appName.toLower()+"-"+version;// TODO what for? change "dest" to that?
192 
193  KArchive* arch = 0;
194  if( templateArchive.endsWith(".zip") ) {
195  arch = new KZip(templateArchive);
196  } else {
197  arch = new KTar(templateArchive, "application/x-bzip");
198  }
199  if (arch->open(QIODevice::ReadOnly)) {
200  if( !QFileInfo( dest.toLocalFile() ).exists() ) {
201  QDir::root().mkdir( dest.toLocalFile() );
202  }
203  unpackArchive(arch->directory(), dest.toLocalFile());
204  }
205  delete arch;
206 
207  feedback.append(i18n("Succeeded.\n"));
208  ui_generate.label->setText(feedback);
209 
210  QString resume;
211  QString url = field("url").toString();
212  resume = i18n("Your project name is: <b>%1</b>, based on the %2 template.<br />", appName, templateName);
213  resume.append(i18n("Version: %1 <br /><br />", version));
214  resume.append(i18n("Installed in: %1 <br /><br />", url));
215  resume.append(i18n("You will find a README in your project folder <b>%1</b><br /> to help you get started with your project.", url+'/'+appName.toLower()));
216  ui_generate.summaryLabel->setText(resume);
217 }
218 
219 #include "generatepage.moc"
GeneratePage::GeneratePage
GeneratePage(QWidget *parent=0)
Definition: generatepage.cpp:43
QWizardPage
prefs.h
QWidget
class @6 QWidget
This is the main view class for %{APPNAME}.
kapptemplate.h
generatepage.h
current_year
const QString current_year
Definition: generatepage.cpp:41
version
static const char version[]
Definition: main.cpp:32
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:03:23 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kapptemplate

Skip menu "kapptemplate"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal