2 * Copyright 2006, 2011, Stephan Aßmus <superstippi@gmx.de>.
3 * All rights reserved. Distributed under the terms of the MIT License.
23 #include "CommandStack.h"
28 #undef B_TRANSLATION_CONTEXT
29 #define B_TRANSLATION_CONTEXT "Icon-O-Matic-Exporter"
47 WaitForExportThread();
54 Exporter::Export(Document
* document
, const entry_ref
& ref
)
56 if (!document
|| ref
.name
== NULL
)
60 fClonedIcon
= fDocument
->Icon()->Clone();
66 fExportThread
= spawn_thread(_ExportThreadEntry
, "export",
67 B_NORMAL_PRIORITY
, this);
68 if (fExportThread
< 0)
69 return (status_t
)fExportThread
;
71 resume_thread(fExportThread
);
78 Exporter::SetSelfDestroy(bool selfDestroy
)
80 fSelfDestroy
= selfDestroy
;
85 Exporter::WaitForExportThread()
87 if (fExportThread
>= 0 && find_thread(NULL
) != fExportThread
) {
89 wait_for_thread(fExportThread
, &ret
);
99 Exporter::_ExportThreadEntry(void* cookie
)
101 Exporter
* exporter
= (Exporter
*)cookie
;
102 return exporter
->_ExportThread();
107 Exporter::_ExportThread()
109 status_t ret
= _Export(fClonedIcon
, &fRef
);
111 // inform user of failure at this point
112 BString
helper(B_TRANSLATE("Saving your document failed!"));
113 helper
<< "\n\n" << B_TRANSLATE("Error: ") << strerror(ret
);
114 BAlert
* alert
= new BAlert(B_TRANSLATE("bad news"), helper
.String(),
115 B_TRANSLATE_CONTEXT("Bleep!",
116 "Exporter - Continue in error dialog"),
118 // launch alert asynchronously
119 alert
->SetFlags(alert
->Flags() | B_CLOSE_ON_ESCAPE
);
124 // add to recent document list
125 be_roster
->AddToRecentDocuments(&fRef
);
126 // mark command stack state as saved,
127 fDocument
->CommandStack()->Save();
128 // NOTE: CommandStack is thread safe
130 if (fDocument
->WriteLock()) {
131 // set ref and name of document
132 // fDocument->SetRef(fRef);
133 fDocument
->SetName(fRef
.name
);
135 fDocument
->WriteUnlock();
147 Exporter::_Export(const Icon
* icon
, const entry_ref
* docRef
)
149 // TODO: reenable the commented out code, but make it work
150 // the opposite direction, ie *copy* the file contents
152 BEntry
entry(docRef
, true);
153 if (entry
.IsDirectory())
156 const entry_ref
* ref
= docRef
;
157 // entry_ref tempRef;
159 // if (entry.Exists()) {
160 // // if the file exists create a temporary file in the same folder
161 // // and hope that it doesn't already exist...
162 // BPath tempPath(docRef);
163 // if (tempPath.GetParent(&tempPath) >= B_OK) {
164 // BString helper(docRef->name);
165 // helper << system_time();
166 // if (tempPath.Append(helper.String()) >= B_OK
167 // && entry.SetTo(tempPath.Path()) >= B_OK
168 // && entry.GetRef(&tempRef) >= B_OK) {
169 // // have the output ref point to the temporary
176 status_t ret
= B_BAD_VALUE
;
178 // do the actual save operation into a file
179 BFile
outFile(ref
, B_CREATE_FILE
| B_READ_WRITE
| B_ERASE_FILE
);
180 ret
= outFile
.InitCheck();
183 // export using the virtual Export() version
184 ret
= Export(icon
, &outFile
);
186 printf("Exporter::_Export() - "
187 "unkown exception occured!\n");
191 printf("Exporter::_Export() - "
192 "failed to export icon: %s\n", strerror(ret
));
195 printf("Exporter::_Export() - "
196 "failed to create output file: %s\n", strerror(ret
));
200 // if (ret < B_OK && ref != docRef) {
201 // // in case of failure, remove temporary file
205 // if (ret >= B_OK && ref != docRef) {
206 // // move temp file overwriting actual document file
207 // BEntry docEntry(docRef, true);
208 // // copy attributes of previous document file
209 // BNode sourceNode(&docEntry);
210 // BNode destNode(&entry);
211 // if (sourceNode.InitCheck() >= B_OK && destNode.InitCheck() >= B_OK) {
213 // if (sourceNode.Lock() >= B_OK) {
214 // if (destNode.Lock() >= B_OK) {
215 // // iterate over the attributes
216 // char attrName[B_ATTR_NAME_LENGTH];
217 // while (sourceNode.GetNextAttrName(attrName) >= B_OK) {
218 //// // skip the icon, since we probably wrote that
220 //// if (strcmp(attrName, "BEOS:ICON") == 0)
223 // if (sourceNode.GetAttrInfo(attrName, &info) >= B_OK) {
224 // char *buffer = new (nothrow) char[info.size];
225 // if (buffer && sourceNode.ReadAttr(attrName, info.type, 0,
226 // buffer, info.size) == info.size) {
227 // destNode.WriteAttr(attrName, info.type, 0,
228 // buffer, info.size);
233 // destNode.Unlock();
235 // sourceNode.Unlock();
238 // // clobber the orginal file with the new temporary one
239 // ret = entry.Rename(docRef->name, true);
242 if (ret
>= B_OK
&& MIMEType()) {
245 if (node
.InitCheck() == B_OK
) {
246 BNodeInfo
nodeInfo(&node
);
247 if (nodeInfo
.InitCheck() == B_OK
)
248 nodeInfo
.SetType(MIMEType());