Back to index

lightning-sunbird  0.9+nobinonly
nsFileSelector.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Samir Gehani <sgehani@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039  
00040  
00041 #ifndef _NS_FILESELECTOR_H_
00042        #include "nsFileSelector.h"
00043 #endif
00044 
00045 #include "nsAppleSingleDecoder.h"
00046 
00047 nsFileSelector::nsFileSelector()
00048 {
00049        mFile = NULL;
00050 }
00051 
00052 nsFileSelector::~nsFileSelector()
00053 {
00054 
00055 }
00056 
00057 pascal void
00058 OurNavEventFunction(NavEventCallbackMessage callBackSelector, NavCBRecPtr callBackParms,
00059                                    NavCallBackUserData callBackUD)
00060 {
00061        WindowPtr  windowPtr;
00062                      
00063        windowPtr = (WindowPtr) callBackParms->eventData.eventDataParms.event->message;
00064        if (!windowPtr)
00065               return;
00066               
00067        switch(callBackSelector)
00068        {
00069               case kNavCBEvent:
00070                      switch(callBackParms->eventData.eventDataParms.event->what)
00071                      {
00072                             case updateEvt:
00073                                    if(((WindowPeek) windowPtr)->windowKind != kDialogWindowKind)
00074                                           // XXX irrelevant
00075                                           // HandleUpdateEvt((EventRecord *) callBackParms->eventData.eventDataParms.event); 
00076                                    break;
00077                      }
00078                      break;
00079        } 
00080 }
00081 
00082 OSErr
00083 nsFileSelector::SelectFile(FSSpecPtr aOutFile)
00084 {
00085        OSErr  err = noErr;
00086        NavReplyRecord              reply; 
00087        NavDialogOptions     dlgOpts;
00088        NavEventUPP                 eventProc;
00089        AEDesc                      resultDesc, initDesc;
00090        FSSpec                      tmp;
00091        short                       cwdVRefNum;
00092        long                        cwdDirID, len;
00093        
00094        mFile = aOutFile;
00095        
00096        err = NavGetDefaultDialogOptions(&dlgOpts);
00097        len = strlen("Please select a file");
00098        nsAppleSingleDecoder::PLstrncpy(dlgOpts.message, "\pPlease select a file", len);
00099        eventProc = NewNavEventProc( (ProcPtr) OurNavEventFunction );
00100        
00101        ERR_CHECK( GetCWD(&cwdDirID, &cwdVRefNum) );
00102        ERR_CHECK( FSMakeFSSpec(cwdVRefNum, cwdDirID, NULL, &tmp) );
00103        ERR_CHECK( AECreateDesc(typeFSS, (void*) &tmp, sizeof(FSSpec), &initDesc) );
00104        
00105        err = NavChooseFile( &initDesc, &reply, &dlgOpts, eventProc, NULL, NULL, NULL, NULL );     
00106               
00107        AEDisposeDesc(&initDesc);
00108        DisposeRoutineDescriptor(eventProc);
00109               
00110        if((reply.validRecord) && (err == noErr))
00111        {
00112               if((err = AECoerceDesc(&(reply.selection),typeFSS,&resultDesc)) == noErr)
00113               {
00114                      BlockMoveData(*resultDesc.dataHandle,&tmp,sizeof(FSSpec));
00115                      /* forces name to get filled */
00116                      FSMakeFSSpec(tmp.vRefNum, tmp.parID, tmp.name, aOutFile); 
00117               }
00118             
00119               AEDisposeDesc(&resultDesc);
00120               NavDisposeReply(&reply);
00121        }
00122        
00123        return err;
00124 }
00125 
00126 OSErr
00127 nsFileSelector::SelectFolder(FSSpecPtr aOutFolder)
00128 {
00129        OSErr  err = noErr;
00130        NavReplyRecord              reply; 
00131        NavDialogOptions     dlgOpts;
00132        NavEventUPP                 eventProc;
00133        AEDesc                      resultDesc, initDesc;
00134        FSSpec                      tmp;
00135        short                       cwdVRefNum;
00136        long                        cwdDirID, len;
00137        
00138        mFile = aOutFolder;
00139        
00140        err = NavGetDefaultDialogOptions(&dlgOpts);
00141        len = strlen("Please select a folder");
00142        nsAppleSingleDecoder::PLstrncpy(dlgOpts.message, "\pPlease select a folder", len);
00143        eventProc = NewNavEventProc( (ProcPtr) OurNavEventFunction );
00144        
00145        ERR_CHECK( GetCWD(&cwdDirID, &cwdVRefNum) );
00146        ERR_CHECK( FSMakeFSSpec(cwdVRefNum, cwdDirID, NULL, &tmp) );
00147        ERR_CHECK( AECreateDesc(typeFSS, (void*) &tmp, sizeof(FSSpec), &initDesc) );
00148        
00149        err = NavChooseFolder( &initDesc, &reply, &dlgOpts, eventProc, NULL, NULL );
00150               
00151        AEDisposeDesc(&initDesc);
00152        DisposeRoutineDescriptor(eventProc);
00153               
00154        if((reply.validRecord) && (err == noErr))
00155        {
00156               if((err = AECoerceDesc(&(reply.selection),typeFSS,&resultDesc)) == noErr)
00157               {
00158                      BlockMoveData(*resultDesc.dataHandle,&tmp,sizeof(FSSpec));
00159                      /* forces name to get filled */
00160                      FSMakeFSSpec(tmp.vRefNum, tmp.parID, tmp.name, aOutFolder); 
00161               }
00162             
00163               AEDisposeDesc(&resultDesc);
00164               NavDisposeReply(&reply);
00165        }
00166        
00167        return err;
00168 }
00169 
00170 OSErr
00171 nsFileSelector::GetCWD(long *aOutDirID, short *aOutVRefNum)
00172 {
00173        OSErr                       err = noErr;
00174        ProcessSerialNumber  psn;
00175        ProcessInfoRec              pInfo;
00176        FSSpec                      tmp;
00177        
00178        /* get cwd based on curr ps info */
00179        if (!(err = GetCurrentProcess(&psn))) 
00180        {
00181               pInfo.processName = nil;
00182               pInfo.processAppSpec = &tmp;
00183               pInfo.processInfoLength = (sizeof(ProcessInfoRec));
00184               
00185               if(!(err = GetProcessInformation(&psn, &pInfo)))
00186               {
00187                      *aOutDirID = pInfo.processAppSpec->parID;
00188                      *aOutVRefNum = pInfo.processAppSpec->vRefNum;
00189               }
00190        }
00191        
00192        return err;
00193 }