Rev 11781 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4430 | cbradney | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
3201 | subik | 7 | #include "collect4output.h" |
8 | #include "collect4output.moc" |
||
9 | |||
10 | #include "scribus.h" |
||
11 | #include "scribusapp.h" |
||
12 | #include "util.h" |
||
13 | #include "prefscontext.h" |
||
14 | #include "prefsfile.h" |
||
15 | #include "prefsmanager.h" |
||
16 | #include "commonstrings.h" |
||
17 | #include "undomanager.h" |
||
18 | #include "filewatcher.h" |
||
19 | #include "pageitem.h" |
||
3251 | craig | 20 | #include "scraction.h" |
3597 | subik | 21 | #include "scraction.h" |
3201 | subik | 22 | |
23 | #include <qmessagebox.h> |
||
24 | #include <qstring.h> |
||
25 | #include <qmap.h> |
||
26 | #include <qdir.h> |
||
27 | |||
28 | extern ScribusQApp* ScQApp; |
||
29 | |||
30 | |||
31 | CollectForOutput::CollectForOutput(bool withFonts, bool compressDoc) |
||
32 | : QObject(ScQApp, 0) |
||
33 | { |
||
34 | outputDirectory = QString(); |
||
35 | compressDoc = compressDoc; |
||
36 | withFonts = withFonts; |
||
37 | dirs = PrefsManager::instance()->prefsFile->getContext("dirs"); |
||
38 | collectedFiles.clear(); |
||
39 | } |
||
40 | |||
41 | bool CollectForOutput::newDirDialog() |
||
42 | { |
||
43 | QString curDir = QDir::currentDirPath(); |
||
44 | if (ScQApp->usingGUI()) |
||
45 | { |
||
46 | QString wdir = "."; |
||
47 | QString prefsDocDir = PrefsManager::instance()->documentDir(); |
||
48 | if (!prefsDocDir.isEmpty()) |
||
49 | wdir = dirs->get("collect", prefsDocDir); |
||
50 | else |
||
51 | wdir = dirs->get("collect", "."); |
||
4026 | craig | 52 | outputDirectory = ScMW->CFileDialog(wdir, tr("Choose a Directory"), "", "", false, false, false, false, true, &compressDoc, &withFonts); |
3201 | subik | 53 | } |
54 | if (outputDirectory.isEmpty()) |
||
55 | return false; |
||
4219 | fschmid | 56 | if (!outputDirectory.endsWith("/")) |
57 | outputDirectory += "/"; |
||
3201 | subik | 58 | return true; |
59 | } |
||
60 | |||
61 | QString CollectForOutput::collect() |
||
62 | { |
||
3597 | subik | 63 | if (!newDirDialog()) |
3201 | subik | 64 | return ""; |
4026 | craig | 65 | ScMW->fileWatcher->forceScan(); |
66 | ScMW->fileWatcher->stop(); |
||
3599 | fschmid | 67 | dirs->set("collect", outputDirectory.left(outputDirectory.findRev("/",-2))); |
7121 | fschmid | 68 | ScMW->mainWindowStatusLabel->setText( tr("Collecting...")); |
3931 | subik | 69 | |
5453 | subik | 70 | if (!collectItems()) |
3931 | subik | 71 | { |
5453 | subik | 72 | QMessageBox::warning(ScMW, tr("Warning"), "<qt>" + tr("Cannot collect all files for output for file:\n%1").arg(newName) + "</qt>", CommonStrings::tr_OK); |
3931 | subik | 73 | return ""; |
74 | } |
||
75 | |||
5453 | subik | 76 | /* collect document must go last because of image paths changes |
77 | in collectItems() */ |
||
78 | if (!collectDocument()) |
||
3201 | subik | 79 | { |
5453 | subik | 80 | QMessageBox::warning(ScMW, CommonStrings::trWarning, "<qt>" + tr("Cannot collect the file: \n%1").arg(newName) + "</qt>", CommonStrings::tr_OK); |
3201 | subik | 81 | return ""; |
82 | } |
||
3931 | subik | 83 | |
3755 | cbradney | 84 | if (withFonts) |
85 | collectFonts(); |
||
3931 | subik | 86 | |
3201 | subik | 87 | QDir::setCurrent(outputDirectory); |
4026 | craig | 88 | ScMW->updateActiveWindowCaption(newName); |
3201 | subik | 89 | UndoManager::instance()->renameStack(newName); |
4026 | craig | 90 | ScMW->scrActions["fileSave"]->setEnabled(false); |
91 | ScMW->scrActions["fileRevert"]->setEnabled(false); |
||
92 | ScMW->updateRecent(newName); |
||
93 | ScMW->mainWindowStatusLabel->setText(""); |
||
94 | ScMW->mainWindowProgressBar->reset(); |
||
95 | ScMW->fileWatcher->start(); |
||
3201 | subik | 96 | collectedFiles.clear(); |
97 | return newName; |
||
98 | } |
||
99 | |||
100 | bool CollectForOutput::collectDocument() |
||
101 | { |
||
102 | QFileInfo fi = QFileInfo(outputDirectory); |
||
103 | newName = outputDirectory; |
||
104 | if (!fi.exists()) |
||
105 | return false; |
||
106 | if (!fi.isDir() || !fi.isWritable()) |
||
107 | return false; |
||
108 | |||
4026 | craig | 109 | if (ScMW->doc->hasName) |
3201 | subik | 110 | { |
4026 | craig | 111 | QFileInfo fis(ScMW->doc->DocName); |
3201 | subik | 112 | newName += fis.fileName(); |
113 | } |
||
114 | else |
||
4026 | craig | 115 | newName += ScMW->doc->DocName+".sla"; |
3201 | subik | 116 | |
4026 | craig | 117 | ScMW->doc->hasName = true; |
3201 | subik | 118 | if (compressDoc) |
119 | { |
||
120 | if (!newName.endsWith(".gz")) |
||
121 | newName += ".gz"; |
||
122 | else |
||
123 | { |
||
124 | if (newName.endsWith(".gz")) |
||
125 | newName = newName.remove(".gz"); |
||
126 | } |
||
127 | } |
||
128 | |||
4026 | craig | 129 | if (!overwrite(ScMW, newName)) |
3201 | subik | 130 | return false; |
4026 | craig | 131 | if (!ScMW->DoFileSave(newName)) |
3201 | subik | 132 | return false; |
133 | return true; |
||
134 | } |
||
135 | |||
136 | bool CollectForOutput::collectItems() |
||
137 | { |
||
138 | uint counter = 0; |
||
139 | for (uint lc = 0; lc < 3; ++lc) |
||
140 | { |
||
141 | PageItem* ite; |
||
142 | switch (lc) |
||
143 | { |
||
144 | case 0: |
||
4026 | craig | 145 | counter = ScMW->doc->MasterItems.count(); |
3201 | subik | 146 | break; |
147 | case 1: |
||
4026 | craig | 148 | counter = ScMW->doc->DocItems.count(); |
3201 | subik | 149 | break; |
150 | case 2: |
||
4026 | craig | 151 | counter = ScMW->doc->FrameItems.count(); |
3201 | subik | 152 | break; |
153 | } |
||
154 | for (uint b = 0; b < counter; ++b) |
||
155 | { |
||
156 | switch (lc) |
||
157 | { |
||
158 | case 0: |
||
4026 | craig | 159 | ite = ScMW->doc->MasterItems.at(b); |
3201 | subik | 160 | break; |
161 | case 1: |
||
4026 | craig | 162 | ite = ScMW->doc->DocItems.at(b); |
3201 | subik | 163 | break; |
164 | case 2: |
||
4026 | craig | 165 | ite = ScMW->doc->FrameItems.at(b); |
3201 | subik | 166 | break; |
167 | } |
||
3625 | avox | 168 | if (ite->asImageFrame()) |
3201 | subik | 169 | { |
3931 | subik | 170 | /* hack for subsequent c4o "./" -> "/doc/full/path" */ |
171 | QString ofName(ite->Pfile); |
||
172 | QFileInfo itf = QFileInfo(ofName); |
||
173 | if (!itf.exists()) |
||
174 | { |
||
175 | ofName = QDir::convertSeparators(PrefsManager::instance()->documentDir() + "/" + ofName); |
||
176 | itf.setFile(ofName); |
||
177 | } |
||
178 | // end of hack |
||
3201 | subik | 179 | if (itf.exists()) |
180 | { |
||
3931 | subik | 181 | QString oldFile = ofName; |
3598 | subik | 182 | ite->Pfile = collectFile(oldFile, itf.fileName()); |
4026 | craig | 183 | if (ScMW->fileWatcherActive()) |
3201 | subik | 184 | { |
4026 | craig | 185 | ScMW->fileWatcher->removeFile(oldFile); |
186 | ScMW->fileWatcher->addFile(ite->Pfile); |
||
3201 | subik | 187 | } |
188 | } |
||
189 | } |
||
3625 | avox | 190 | if (ite->asTextFrame()) |
3201 | subik | 191 | { |
4084 | cbradney | 192 | if (ite->isAnnotation()) |
3201 | subik | 193 | { |
194 | QFileInfo itf; |
||
195 | if (!ite->Pfile.isEmpty()) |
||
196 | { |
||
197 | itf = QFileInfo(ite->Pfile); |
||
198 | if (itf.exists()) |
||
199 | { |
||
200 | QString oldFile = ite->Pfile; |
||
3598 | subik | 201 | ite->Pfile = collectFile(oldFile, itf.fileName()); |
4026 | craig | 202 | if (ScMW->fileWatcherActive()) |
3201 | subik | 203 | { |
4026 | craig | 204 | ScMW->fileWatcher->removeFile(oldFile); |
205 | ScMW->fileWatcher->addFile(ite->Pfile); |
||
3201 | subik | 206 | } |
207 | } |
||
208 | } |
||
209 | if (!ite->Pfile2.isEmpty()) |
||
210 | { |
||
211 | itf = QFileInfo(ite->Pfile2); |
||
212 | if (itf.exists()) |
||
3598 | subik | 213 | ite->Pfile2 = collectFile(ite->Pfile2, itf.fileName()); |
3201 | subik | 214 | } |
215 | if (!ite->Pfile3.isEmpty()) |
||
216 | { |
||
217 | itf = QFileInfo(ite->Pfile3); |
||
218 | if (itf.exists()) |
||
3598 | subik | 219 | ite->Pfile3 = collectFile(ite->Pfile3, itf.fileName()); |
3201 | subik | 220 | } |
221 | } |
||
222 | } |
||
223 | } |
||
224 | } |
||
225 | return true; |
||
226 | } |
||
227 | |||
228 | bool CollectForOutput::collectFonts() |
||
229 | { |
||
230 | PrefsManager *prefsManager = PrefsManager::instance(); |
||
3544 | avox | 231 | QMap<QString,int>::Iterator it3; |
4026 | craig | 232 | QMap<QString,int>::Iterator it3end = ScMW->doc->UsedFonts.end(); |
233 | for (it3 = ScMW->doc->UsedFonts.begin(); it3 != it3end; ++it3) |
||
3201 | subik | 234 | { |
3500 | avox | 235 | QFileInfo itf = QFileInfo(prefsManager->appPrefs.AvailFonts[it3.key()]->fontFilePath()); |
236 | copyFile(prefsManager->appPrefs.AvailFonts[it3.key()]->fontFilePath(), outputDirectory + itf.fileName()); |
||
11827 | jghali | 237 | if (prefsManager->appPrefs.AvailFonts[it3.key()]->typeCode == Foi::TYPE1) |
238 | { |
||
239 | QStringList metrics; |
||
240 | QString fontDir = itf.absFilePath(); |
||
241 | QString fontFile = itf.fileName(); |
||
242 | metrics += findFontMetrics(fontDir, fontFile); |
||
243 | if ( metrics.size() <= 0 ) |
||
244 | { |
||
245 | QDir dir; |
||
246 | if (dir.exists(fontDir + "/AFMs")) |
||
247 | metrics += findFontMetrics(fontDir + "/AFMs", fontFile); |
||
248 | if (dir.exists(fontDir + "/afm") && metrics.size() <= 0) |
||
249 | metrics += findFontMetrics(fontDir + "/afm", fontFile); |
||
250 | if (dir.exists(fontDir + "/Pfm") && metrics.size() <= 0) |
||
251 | metrics += findFontMetrics(fontDir + "/Pfm", fontFile); |
||
252 | if (dir.exists(fontDir + "/pfm") && metrics.size() <= 0) |
||
253 | metrics += findFontMetrics(fontDir + "/pfm", fontFile); |
||
254 | } |
||
255 | for (uint a = 0; a < metrics.size(); a++) |
||
256 | { |
||
257 | QString origAFM = metrics[a]; |
||
258 | QFileInfo fi(origAFM); |
||
259 | copyFile(origAFM, outputDirectory + fi.fileName()); |
||
260 | } |
||
261 | } |
||
3201 | subik | 262 | } |
263 | return true; |
||
264 | } |
||
265 | |||
11827 | jghali | 266 | QStringList CollectForOutput::findFontMetrics(const QString& baseDir, const QString& baseName) const |
267 | { |
||
268 | QStringList metricsFiles; |
||
269 | QString basePath = baseDir + "/" + baseName; |
||
270 | QString afnm = basePath.left(basePath.length()-3); |
||
271 | // Look for afm files |
||
272 | QString afmName(afnm+"afm"); |
||
273 | if(QFile::exists(afmName)) |
||
274 | metricsFiles.append(afmName); |
||
275 | else |
||
276 | { |
||
277 | afmName = afnm+"Afm"; |
||
278 | if(QFile::exists(afmName)) |
||
279 | metricsFiles.append(afmName); |
||
280 | else |
||
281 | { |
||
282 | afmName = afnm+"AFM"; |
||
283 | if(QFile::exists(afmName)) |
||
284 | metricsFiles.append(afmName); |
||
285 | } |
||
286 | } |
||
287 | // Look for pfm files |
||
288 | QString pfmName(afnm+"pfm"); |
||
289 | if(QFile::exists(pfmName)) |
||
290 | metricsFiles.append(pfmName); |
||
291 | else |
||
292 | { |
||
293 | pfmName = afnm+"Pfm"; |
||
294 | if(QFile::exists(pfmName)) |
||
295 | metricsFiles.append(pfmName); |
||
296 | else |
||
297 | { |
||
298 | afmName = afnm+"PFM"; |
||
299 | if(QFile::exists(pfmName)) |
||
300 | metricsFiles.append(pfmName); |
||
301 | } |
||
302 | } |
||
303 | return metricsFiles; |
||
304 | } |
||
305 | |||
3201 | subik | 306 | QString CollectForOutput::collectFile(QString oldFile, QString newFile) |
307 | { |
||
308 | uint cnt = 1; |
||
309 | bool copy = true; |
||
3931 | subik | 310 | |
3201 | subik | 311 | while (collectedFiles.contains(newFile)) |
312 | { |
||
313 | // overwrite only different sources |
||
314 | if (collectedFiles[newFile] == oldFile) |
||
315 | { |
||
316 | copy = false; |
||
317 | break; |
||
318 | } |
||
319 | QFileInfo fi(newFile); |
||
320 | QString basename = fi.baseName().left(fi.baseName().findRev("_")); |
||
3598 | subik | 321 | newFile = QString("%1_%2.%3").arg(basename).arg(cnt).arg(fi.extension()); |
3201 | subik | 322 | ++cnt; |
323 | } |
||
324 | if (copy) |
||
4532 | subik | 325 | copyFile(oldFile, outputDirectory + newFile); |
3201 | subik | 326 | collectedFiles[newFile] = oldFile; |
11781 | mrdocs | 327 | return outputDirectory + newFile; |
3201 | subik | 328 | } |