Back to index

scribus-ng  1.3.4.dfsg+svn20071115
collect4output.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include "collect4output.h"
00008 #include "collect4output.moc"
00009 
00010 #include "scribusdoc.h"
00011 #include "scribuscore.h"
00012 #include "util.h"
00013 #include "prefscontext.h"
00014 #include "prefsfile.h"
00015 #include "prefsmanager.h"
00016 #include "commonstrings.h"
00017 #include "undomanager.h"
00018 #include "filewatcher.h"
00019 #include "pageitem.h"
00020 #include "scraction.h"
00021 #include "scpattern.h"
00022 
00023 #include <qmessagebox.h>
00024 #include <qstring.h>
00025 #include <qmap.h>
00026 #include <qdir.h>
00027 
00028 CollectForOutput::CollectForOutput(ScribusDoc* doc, bool withFontsA, bool withProfilesA, bool compressDocA)
00029        : QObject(ScCore, 0),
00030        m_Doc(0)
00031 {
00032        m_Doc=doc;
00033        outputDirectory = QString();
00034        compressDoc = compressDocA;
00035        withFonts = withFontsA;
00036        withProfiles = withProfilesA;
00037        dirs = PrefsManager::instance()->prefsFile->getContext("dirs");
00038        collectedFiles.clear();
00039 }
00040 
00041 bool CollectForOutput::newDirDialog()
00042 {
00043        QString curDir = QDir::currentDirPath();
00044        if (ScCore->usingGUI())
00045        {
00046               QString wdir = ".";
00047               QString prefsDocDir = PrefsManager::instance()->documentDir();
00048               if (!prefsDocDir.isEmpty())
00049                      wdir = dirs->get("collect", prefsDocDir);
00050               else
00051                      wdir = dirs->get("collect", ".");
00052               outputDirectory = ScCore->primaryMainWindow()->CFileDialog(wdir, tr("Choose a Directory"), "", "", fdDirectoriesOnly, &compressDoc, &withFonts, &withProfiles);
00053        }
00054        if (outputDirectory.isEmpty())
00055               return false;
00056        if (!outputDirectory.endsWith("/"))
00057               outputDirectory += "/";
00058        return true;
00059 }
00060 
00061 QString CollectForOutput::collect()
00062 {
00063        if (!newDirDialog())
00064               return "";
00065        ScCore->fileWatcher->forceScan();
00066        ScCore->fileWatcher->stop();
00067        dirs->set("collect", outputDirectory.left(outputDirectory.findRev("/",-2)));
00068        ScCore->primaryMainWindow()->setStatusBarInfoText( tr("Collecting..."));
00069 
00070        if (!collectItems())
00071        {
00072               QMessageBox::warning(ScCore->primaryMainWindow(), CommonStrings::trWarning,
00073                                                   "<qt>" + tr("Cannot collect all files for output for file:\n%1").arg(newName) + "</qt>",
00074                                                   CommonStrings::tr_OK);
00075               return "";
00076        }
00077 
00078        if (withFonts)
00079               collectFonts();
00080        if (withProfiles)
00081               collectProfiles();
00082 
00083        /* collect document must go last because of image paths changes
00084        in collectItems() */
00085        if (!collectDocument())
00086        {
00087               QMessageBox::warning(ScCore->primaryMainWindow(), CommonStrings::trWarning, "<qt>" + tr("Cannot collect the file: \n%1").arg(newName) + "</qt>", CommonStrings::tr_OK);
00088               return "";
00089        }
00090 
00091        QDir::setCurrent(outputDirectory);
00092        ScCore->primaryMainWindow()->updateActiveWindowCaption(newName);
00093        UndoManager::instance()->renameStack(newName);
00094        ScCore->primaryMainWindow()->scrActions["fileSave"]->setEnabled(false);
00095        ScCore->primaryMainWindow()->scrActions["fileRevert"]->setEnabled(false);
00096        ScCore->primaryMainWindow()->updateRecent(newName);
00097        ScCore->primaryMainWindow()->setStatusBarInfoText("");
00098        ScCore->primaryMainWindow()->mainWindowProgressBar->reset();
00099        ScCore->fileWatcher->start();
00100        collectedFiles.clear();
00101        return newName;
00102 }
00103 
00104 bool CollectForOutput::collectDocument()
00105 {
00106        QFileInfo fi = QFileInfo(outputDirectory);
00107        newName = outputDirectory;
00108        if (!fi.exists())
00109               return false;
00110        if (!fi.isDir() || !fi.isWritable())
00111               return false;
00112 
00113        if (m_Doc->hasName)
00114        {
00115               QFileInfo fis(m_Doc->DocName);
00116               newName += fis.fileName();
00117        }
00118        else
00119               newName += m_Doc->DocName+".sla";
00120 
00121        m_Doc->hasName = true;
00122        if (compressDoc)
00123        {
00124               if (!newName.endsWith(".gz"))
00125                      newName += ".gz";
00126               else
00127               {
00128                      if (newName.endsWith(".gz"))
00129                             newName = newName.remove(".gz");
00130               }
00131        }
00132 
00133        if (!overwrite(ScCore->primaryMainWindow(), newName))
00134               return false;
00135        if (!ScCore->primaryMainWindow()->DoFileSave(newName))
00136               return false;
00137        return true;
00138 }
00139 
00140 bool CollectForOutput::collectItems()
00141 {
00142        uint counter = 0;
00143        for (uint lc = 0; lc < 3; ++lc)
00144        {
00145               PageItem* ite = NULL;
00146               switch (lc)
00147               {
00148                      case 0:
00149                             counter = m_Doc->MasterItems.count();
00150                             break;
00151                      case 1:
00152                             counter = m_Doc->DocItems.count();
00153                             break;
00154                      case 2:
00155                             counter = m_Doc->FrameItems.count();
00156                             break;
00157               }
00158               for (uint b = 0; b < counter; ++b)
00159               {
00160                      switch (lc)
00161                      {
00162                             case 0:
00163                                    ite = m_Doc->MasterItems.at(b);
00164                                    break;
00165                             case 1:
00166                                    ite = m_Doc->DocItems.at(b);
00167                                    break;
00168                             case 2:
00169                                    ite = m_Doc->FrameItems.at(b);
00170                                    break;
00171                      }
00172                      processItem(ite);
00173               }
00174        }
00175        QStringList patterns = m_Doc->getUsedPatterns();
00176        for (uint c = 0; c < patterns.count(); ++c)
00177        {
00178               ScPattern pa = m_Doc->docPatterns[patterns[c]];
00179               for (uint o = 0; o < pa.items.count(); o++)
00180               {
00181                      processItem(pa.items.at(o));
00182               }
00183        }
00184        return true;
00185 }
00186 
00187 void CollectForOutput::processItem(PageItem *ite)
00188 {
00189        if (ite->asImageFrame())
00190        {
00191               /* hack for subsequent c4o "./" -> "/doc/full/path" */
00192               QString ofName(ite->Pfile);
00193               QFileInfo itf = QFileInfo(ofName);
00194               if (!itf.exists())
00195               {
00196                      ofName = QDir::convertSeparators(PrefsManager::instance()->documentDir() + "/" + ofName);
00197                      itf.setFile(ofName);
00198               }
00199               // end of hack
00200               if (itf.exists())
00201               {
00202                      QString oldFile = ofName;
00203                      ite->Pfile = collectFile(oldFile, itf.fileName());
00204                      ScCore->fileWatcher->removeFile(oldFile);
00205                      ScCore->fileWatcher->addFile(ite->Pfile);
00206               }
00207        }
00208        if (ite->asTextFrame())
00209        {
00210               if (ite->isAnnotation())
00211               {
00212                      QFileInfo itf;
00213                      if (!ite->Pfile.isEmpty())
00214                      {
00215                             itf = QFileInfo(ite->Pfile);
00216                             if (itf.exists())
00217                             {
00218                                    QString oldFile = ite->Pfile;
00219                                    ite->Pfile = collectFile(oldFile, itf.fileName());
00220                                    ScCore->fileWatcher->removeFile(oldFile);
00221                                    ScCore->fileWatcher->addFile(ite->Pfile);
00222                             }
00223                      }
00224                      if (!ite->Pfile2.isEmpty())
00225                      {
00226                             itf = QFileInfo(ite->Pfile2);
00227                             if (itf.exists())
00228                                    ite->Pfile2 = collectFile(ite->Pfile2, itf.fileName());
00229                      }
00230                      if (!ite->Pfile3.isEmpty())
00231                      {
00232                             itf = QFileInfo(ite->Pfile3);
00233                             if (itf.exists())
00234                                    ite->Pfile3 = collectFile(ite->Pfile3, itf.fileName());
00235                      }
00236               }
00237        }
00238 }
00239 
00240 bool CollectForOutput::collectFonts()
00241 {
00242        PrefsManager *prefsManager = PrefsManager::instance();
00243        QMap<QString,int>::Iterator it3;
00244        QMap<QString,int>::Iterator it3end = m_Doc->UsedFonts.end();
00245        for (it3 = m_Doc->UsedFonts.begin(); it3 != it3end; ++it3)
00246        {
00247               QFileInfo itf = QFileInfo(prefsManager->appPrefs.AvailFonts[it3.key()].fontFilePath());
00248               copyFile(prefsManager->appPrefs.AvailFonts[it3.key()].fontFilePath(), outputDirectory + itf.fileName());
00249        }
00250        return true;
00251 }
00252 
00253 bool CollectForOutput::collectProfiles()
00254 {
00255        ProfilesL docProfiles;
00256        m_Doc->getUsedProfiles(docProfiles);
00257        ProfilesL::Iterator itend = docProfiles.end();
00258        for (ProfilesL::Iterator it = docProfiles.begin(); it != itend; ++it)
00259        {
00260               QString profileName = it.key();
00261               QString profilePath = it.data();
00262               copyFile(profilePath, outputDirectory + QFileInfo(profilePath).fileName());
00263        }
00264        return true;
00265 }
00266 
00267 QString CollectForOutput::collectFile(QString oldFile, QString newFile)
00268 {
00269        uint cnt = 1;
00270        bool copy = true;
00271 
00272        while (collectedFiles.contains(newFile))
00273        {
00274               // overwrite only different sources
00275               if (collectedFiles[newFile] == oldFile)
00276               {
00277                      copy = false;
00278                      break;
00279               }
00280               QFileInfo fi(newFile);
00281               QString basename = fi.baseName().left(fi.baseName().findRev("_"));
00282               newFile = QString("%1_%2.%3").arg(basename).arg(cnt).arg(fi.extension());
00283               ++cnt;
00284        }
00285        if (copy)
00286               copyFile(oldFile, outputDirectory + newFile);
00287        collectedFiles[newFile] = oldFile;
00288        return newFile;
00289 }