Debian Codegraph @

src/af/xap/cocoa/xap_CocoaDlg_FileOpenSaveAs.cpp

raw
1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2
3 /* AbiSource Application Framework
4 * Copyright (C) 1998 AbiSource, Inc.
5 * Copyright (C) 2001-2003, 2009 Hubert Figuiere
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (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 Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301 USA.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30
31 #include "ut_assert.h"
32 #include "ut_debugmsg.h"
33 #include "ut_misc.h"
34 #include "ut_string.h"
35
36 #include "xap_CocoaDlg_FileOpenSaveAs.h"
37 #include "xap_CocoaDialog_Utilities.h"
38 #include "xap_CocoaApp.h"
39 #include "xap_CocoaFrame.h"
40 #include "xap_Dialog_Id.h"
41 #include "xap_Dlg_MessageBox.h"
42 #include "xap_Prefs.h"
43 #include "xap_Strings.h"
44
45 #include "ie_imp.h"
46 #include "ie_impGraphic.h"
47 #include "ie_types.h"
48
49
50 #define PREVIEW_WIDTH 100
51 #define PREVIEW_HEIGHT 100
52
53 @implementation XAP_OpenSavePanel_AccessoryController
54
55 - (id)initWithXAP:(XAP_CocoaDialog_FileOpenSaveAs*)xap
56 {
57 if (![super init]) {
58 return nil;
59 }
60 _xap = xap;
61 return self;
62 }
63
64 - (NSView *)fileTypeAccessoryView
65 {
66 return oFTAccessoryView;
67 }
68
69 - (void)setFileTypeLabel:(const std::string &)label
70 {
71 SetNSControlLabel(oFTLabel, label);
72 }
73
74 - (void)setSelectedFileType:(int)type
75 {
76 [oFTPopUp selectItem:[[oFTPopUp menu] itemWithTag:type]];
77 }
78
79 - (void)removeItemsOfFileTypesMenu
80 {
81 [oFTPopUp removeAllItems];
82 }
83
84 - (void)addItemWithTitle:(NSString *)title fileType:(int)type
85 {
86 [oFTPopUp addItemWithTitle:title];
87 [[oFTPopUp lastItem] setTag:type];
88 }
89
90 - (IBAction)selectFileType:(id)sender
91 {
92 UT_UNUSED(sender);
93 _xap->_setSelectedFileType([[sender selectedItem] tag]);
94 }
95
96 // delegate method
97 #if 0
98 - (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
99 {
100
101 return YES;
102 }
103 #endif
104
105 @end
106
107 /*****************************************************************/
108 static void addSuffixesToFileTypes(NSMutableArray * fileTypes, const char * suffix_list)
109 {
110 std::string suffix;
111 while (const char * ptr1 = strstr(suffix_list, "*.")) {
112 ptr1 += 2;
113 bool bSingleExtension = true;
114 const char * ptr2 = ptr1;
115 while (*ptr2 && (*ptr2 != ';')) {
116 if (*ptr2++ == '.') {
117 bSingleExtension = false;
118 }
119 }
120 suffix.assign(ptr1, ptr2 - ptr1);
121 if (bSingleExtension) {
122 UT_DEBUGMSG(("added suffix %s\n", suffix.c_str()));
123 [fileTypes addObject:[NSString stringWithUTF8String:(suffix.c_str())]];
124 }
125 suffix_list = ptr2;
126 }
127 }
128
129
130 XAP_Dialog * XAP_CocoaDialog_FileOpenSaveAs::static_constructor(XAP_DialogFactory * pFactory, XAP_Dialog_Id dlgid)
131 {
132 XAP_CocoaDialog_FileOpenSaveAs * p = new XAP_CocoaDialog_FileOpenSaveAs(pFactory,dlgid);
133 return p;
134 }
135
136 XAP_CocoaDialog_FileOpenSaveAs::XAP_CocoaDialog_FileOpenSaveAs(XAP_DialogFactory * pDlgFactory, XAP_Dialog_Id dlgid) :
137 XAP_Dialog_FileOpenSaveAs(pDlgFactory,dlgid),
138 m_accessoryViewsController(nil),
139 m_panel(nil),
140 m_fileTypes(nil),
141 m_szFileTypeDescription(0),
142 m_szFileTypeCount(0)
143 {
144 //
145 }
146
147 XAP_CocoaDialog_FileOpenSaveAs::~XAP_CocoaDialog_FileOpenSaveAs(void)
148 {
149 if (m_accessoryViewsController)
150 {
151 [m_accessoryViewsController release];
152 }
153 if (m_fileTypes)
154 {
155 [m_fileTypes release];
156 }
157 }
158
159
160 NSSavePanel * XAP_CocoaDialog_FileOpenSaveAs::_makeOpenPanel()
161 {
162 NSOpenPanel *openPanel = [[NSOpenPanel openPanel] retain];
163
164 [openPanel setAllowsMultipleSelection:NO];
165 [openPanel setCanChooseDirectories:NO];
166 [openPanel setCanChooseFiles:YES];
167 // this call is 10.3 only.
168 if ([openPanel respondsToSelector:@selector(setCanCreateDirectories:)])
169 [openPanel setCanCreateDirectories:NO];
170
171 [openPanel setCanSelectHiddenExtension:NO];
172 [openPanel setExtensionHidden:NO];
173
174 for (UT_uint32 i = 0; i < m_szFileTypeCount; i++) {
175 addSuffixesToFileTypes(m_fileTypes, m_szSuffixes[i]);
176 }
177 if([m_fileTypes count]) {
178 [openPanel setAllowedFileTypes:m_fileTypes];
179 }
180 else {
181 [openPanel setAllowedFileTypes:nil];
182 }
183 return openPanel;
184 }
185
186
187
188 NSSavePanel * XAP_CocoaDialog_FileOpenSaveAs::_makeSavePanel(const std::string & fileTypeLabel)
189 {
190 NSSavePanel * savePanel = [[NSSavePanel savePanel] retain];
191
192 // this call is 10.3 only.
193 if ([savePanel respondsToSelector:@selector(setCanCreateDirectories:)])
194 [savePanel setCanCreateDirectories:YES];
195
196 [savePanel setCanSelectHiddenExtension:YES];
197 [savePanel setExtensionHidden:NO];
198 [m_accessoryViewsController setFileTypeLabel:fileTypeLabel];
199
200 /* File-types PopUp:
201 */
202 [m_accessoryViewsController removeItemsOfFileTypesMenu];
203
204 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
205
206 std::string label;
207 if (pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_FileTypeAutoDetect, label)) {
208 NSString * title = [NSString stringWithUTF8String:(label.c_str())];
209 int type = (int) XAP_DIALOG_FILEOPENSAVEAS_FILE_TYPE_AUTO;
210 [m_accessoryViewsController addItemWithTitle:title fileType:type];
211 }
212
213 UT_sint32 defaultFileType = m_nDefaultFileType;
214
215 for (UT_uint32 i = 0; i < m_szFileTypeCount; i++) {
216 NSString * title = [NSString stringWithUTF8String:m_szDescriptions[i]];
217
218 int type = m_nTypeList[i];
219
220 [m_accessoryViewsController addItemWithTitle:title fileType:type];
221 }
222
223 [m_accessoryViewsController setSelectedFileType:defaultFileType];
224 _setSelectedFileType(defaultFileType);
225
226 [savePanel setAccessoryView:[m_accessoryViewsController fileTypeAccessoryView]];
227 return savePanel;
228 }
229
230 /*****************************************************************/
231
232 void XAP_CocoaDialog_FileOpenSaveAs::runModal(XAP_Frame * /*pFrame*/)
233 {
234 if (!m_accessoryViewsController) {
235 m_accessoryViewsController = [[XAP_OpenSavePanel_AccessoryController alloc] initWithXAP:this];
236 if (m_accessoryViewsController) {
237 if (![NSBundle loadNibNamed:@"xap_CocoaFileOpen_Views" owner:m_accessoryViewsController]) {
238 NSLog (@"Couldn't load nib xap_CocoaFileOpen_Views");
239 [m_accessoryViewsController release];
240 m_accessoryViewsController = 0;
241 }
242 }
243 }
244 UT_ASSERT(m_accessoryViewsController);
245 if (!m_accessoryViewsController)
246 return;
247
248 const XAP_StringSet * pSS = XAP_App::getApp()->getStringSet();
249
250 // do we want to let this function handle stating the Cocoa
251 // directory for writability? Save/Export operations will want
252 // this, open/import will not.
253
254 bool bCheckWritePermission = false;
255 bool bOpenPanel = false;
256 bool bSavePanel = false;
257
258 std::string szTitle;
259 std::string szFileTypeLabel;
260
261 switch (m_id)
262 {
263 case XAP_DIALOG_ID_INSERTMATHML:
264 bOpenPanel = true;
265
266 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_InsertMath, szTitle);
267 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_FileInsertMath, szFileTypeLabel);
268
269 bCheckWritePermission = false;
270 break;
271 case XAP_DIALOG_ID_INSERT_PICTURE:
272 bOpenPanel = true;
273
274 pSS->getValueUTF8(XAP_STRING_ID_DLG_IP_Title, szTitle);
275 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_FileOpenTypeLabel, szFileTypeLabel);
276
277 bCheckWritePermission = false;
278 break;
279 case XAP_DIALOG_ID_FILE_OPEN:
280 bOpenPanel = true;
281
282 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_OpenTitle, szTitle);
283 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_FileOpenTypeLabel, szFileTypeLabel);
284
285 bCheckWritePermission = false;
286 break;
287 case XAP_DIALOG_ID_FILE_SAVEAS:
288 case XAP_DIALOG_ID_FILE_SAVE_IMAGE:
289 bSavePanel = true;
290
291 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_SaveAsTitle, szTitle);
292 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_FileSaveTypeLabel, szFileTypeLabel);
293
294 bCheckWritePermission = true;
295 break;
296 case XAP_DIALOG_ID_FILE_IMPORT:
297 bOpenPanel = true;
298
299 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_ImportTitle, szTitle);
300 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_FileOpenTypeLabel, szFileTypeLabel);
301
302 bCheckWritePermission = false;
303 break;
304 case XAP_DIALOG_ID_FILE_EXPORT:
305 bSavePanel = true;
306
307 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_ExportTitle, szTitle);
308 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_FileSaveTypeLabel, szFileTypeLabel);
309
310 bCheckWritePermission = true;
311 break;
312 case XAP_DIALOG_ID_INSERT_FILE:
313 bOpenPanel = true;
314
315 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_InsertTitle, szTitle);
316 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_FileOpenTypeLabel, szFileTypeLabel);
317
318 bCheckWritePermission = false;
319 break;
320 case XAP_DIALOG_ID_PRINTTOFILE:
321 bSavePanel = true;
322
323 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_PrintToFileTitle, szTitle);
324 pSS->getValueUTF8(XAP_STRING_ID_DLG_FOSA_FilePrintTypeLabel, szFileTypeLabel);
325
326 bCheckWritePermission = true;
327 break;
328 default:
329 UT_ASSERT(UT_SHOULD_NOT_HAPPEN);
330 break;
331 }
332
333 if (!m_fileTypes)
334 m_fileTypes = [[NSMutableArray alloc] initWithCapacity:4];
335 UT_ASSERT(m_fileTypes);
336 if (!m_fileTypes)
337 return;
338
339 m_panel = nil;
340
341 // TODO WTF is this not in XP land?
342 m_szFileTypeCount = g_strv_length((gchar **) m_szDescriptions);
343
344 UT_ASSERT(g_strv_length((gchar **) m_szSuffixes) == m_szFileTypeCount);
345
346 if (bOpenPanel) {
347 m_panel = _makeOpenPanel();
348 }
349 else if (bSavePanel) {
350 m_panel = _makeSavePanel(szFileTypeLabel);
351 }
352 UT_ASSERT(m_panel);
353 if (!m_panel)
354 return;
355
356
357 [m_panel setTitle:[NSString stringWithUTF8String:(szTitle.c_str())]];
358 [m_panel setDelegate:m_accessoryViewsController];
359
360
361 // use the persistence info and/or the suggested filename
362 // to properly seed the dialog.
363
364 NSString * szPersistDirectory = nil;
365 NSString * szPersistFile = nil;
366
367 if (!m_szInitialPathname || !*m_szInitialPathname)
368 {
369 // the caller did not supply initial pathname
370 // (or supplied an empty one). see if we have
371 // some persistent info.
372
373 UT_ASSERT(!m_bSuggestName);
374 if (m_szPersistPathname)
375 {
376 // we have a pathname from a previous use,
377 // extract the directory portion and start
378 // the dialog there (but without a filename).
379
380 szPersistDirectory = [NSString stringWithUTF8String:m_szPersistPathname];
381 }
382 else
383 {
384 // no initial pathname given and we don't have
385 // a pathname from a previous use, so just let
386 // it come up in the current working directory.
387 }
388 }
389 else
390 {
391 // we have an initial pathname (the name of the document
392 // in the frame that we were invoked on). if the caller
393 // wanted us to suggest a filename, use the initial
394 // pathname as is. if not, use the directory portion of
395 // it.
396
397 if (m_bSuggestName)
398 {
399 /* use m_szInitialPathname
400 */
401 NSString * path = [NSString stringWithUTF8String:m_szInitialPathname];
402
403 szPersistDirectory = [path stringByDeletingLastPathComponent];
404
405 if ([szPersistDirectory isEqualToString:path]) // shouldn't happen
406 {
407 szPersistFile = [NSString string];
408 }
409 else
410 {
411 szPersistFile = [path lastPathComponent];
412 }
413
414 /* Slightly odd case where we're saving a file whose original document type
415 * (guessing by the suffix) is not supported for saving to.
416 */
417 if ((m_id == XAP_DIALOG_ID_FILE_SAVEAS) && [szPersistFile length])
418 {
419 NSString * extension = [szPersistFile pathExtension];
420
421 std::string szSaveTypeSuffix = UT_pathSuffix(m_szSuffixes[m_nDefaultFileType]);
422
423 if ([extension length] && !szSaveTypeSuffix.empty())
424 {
425 if (*(szSaveTypeSuffix.c_str()) == '.')
426 {
427 NSString * new_suffix = [NSString stringWithUTF8String:(szSaveTypeSuffix.c_str() + 1)];
428
429 if (![new_suffix isEqualToString:extension])
430 {
431 /* okay, the suggested extension doesn't match the desired extension
432 */
433 NSString * filename = [szPersistFile stringByDeletingPathExtension];
434 szPersistFile = [filename stringByAppendingPathExtension:new_suffix];
435 }
436 }
437 }
438 }
439 }
440 else
441 {
442 /* use directory(m_szInitialPathname)
443 */
444 NSString * path = [NSString stringWithUTF8String:m_szInitialPathname];
445
446 szPersistDirectory = [path stringByDeletingLastPathComponent];
447 szPersistFile = [NSString string];
448 }
449 }
450
451 int result;
452
453 m_bOpenPanel = bOpenPanel;
454 m_bPanelActive = true;
455
456 do
457 {
458 m_bIgnoreCancel = false;
459
460 if (m_bOpenPanel)
461 {
462 // [m_panel setPanelCanOrderOut:YES];
463 NSOpenPanel * openPanel = (NSOpenPanel*)m_panel;
464 if ([m_fileTypes count])
465 {
466 NSString * type = (NSString *) [m_fileTypes objectAtIndex:0];
467 if (szPersistFile)
468 {
469 NSString * extension = [szPersistFile pathExtension];
470 if (![extension isEqualToString:type]) {
471 szPersistFile = nil;
472 }
473 }
474 }
475
476 if ([m_fileTypes count]) {
477 result = [openPanel runModalForDirectory:szPersistDirectory file:szPersistFile types:m_fileTypes];
478 }
479 else {
480 result = [openPanel runModalForDirectory:szPersistDirectory file:szPersistFile types:nil];
481 }
482
483 result = (result == NSOKButton) ? NSFileHandlingPanelOKButton : NSFileHandlingPanelCancelButton;
484 }
485 else
486 {
487 if ([m_fileTypes count])
488 {
489 NSString * type = (NSString *) [m_fileTypes objectAtIndex:0];
490 [m_panel setRequiredFileType:type];
491
492 if (szPersistFile)
493 {
494 NSString * extension = [szPersistFile pathExtension];
495 if ([extension length])
496 {
497 if (![extension isEqualToString:type])
498 {
499 szPersistFile = [szPersistFile stringByDeletingPathExtension];
500 szPersistFile = [szPersistFile stringByAppendingPathExtension:type];
501 }
502 }
503 else
504 {
505 szPersistFile = [szPersistFile stringByAppendingPathExtension:type];
506 }
507 }
508 }
509 else
510 {
511 [m_panel setRequiredFileType:nil];
512 }
513
514 // [m_panel setPanelCanOrderOut:YES];
515 result = [m_panel runModalForDirectory:szPersistDirectory file:szPersistFile];
516 }
517
518 szPersistDirectory = [m_panel directory];
519 szPersistFile = [m_panel filename];
520
521 if (szPersistFile)
522 szPersistFile = [szPersistFile lastPathComponent];
523 }
524 while (m_bIgnoreCancel);
525
526 m_bPanelActive = false;
527
528 szPersistFile = [m_panel filename];
529
530 if ((result == NSFileHandlingPanelOKButton) && szPersistFile)
531 {
532 FREEP (m_szFinalPathname); // g_free before reassigning
533 m_szFinalPathname = g_strdup([szPersistFile UTF8String]);
534 m_answer = a_OK;
535 }
536 }
537
538
539
540 void XAP_CocoaDialog_FileOpenSaveAs::_setSelectedFileType (UT_sint32 type)
541 {
542 m_nFileType = type;
543 m_szFileTypeDescription = 0;
544
545 [m_fileTypes removeAllObjects];
546
547 if (m_nFileType != XAP_DIALOG_FILEOPENSAVEAS_FILE_TYPE_AUTO) {
548 for (UT_uint32 i = 0; i < m_szFileTypeCount; i++) {
549 if (m_nFileType == m_nTypeList[i]) {
550 m_szFileTypeDescription = m_szDescriptions[i];
551
552 addSuffixesToFileTypes(m_fileTypes, m_szSuffixes[i]);
553 }
554 }
555 }
556
557 if([m_fileTypes count]) {
558 [m_panel setAllowedFileTypes:m_fileTypes];
559 }
560 else {
561 [m_panel setAllowedFileTypes:nil];
562 }
563 [m_panel update];
564 // if (m_bPanelActive) {
565 // [m_panel setPanelCanOrderOut:NO];
566
567 // m_bIgnoreCancel = true;
568 // [NSApp stopModal];
569 // }
570 }
571