Back to index

lightning-sunbird  0.9+nobinonly
nsAEDocumentClass.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Simon Fraser <sfraser@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsAEUtils.h"
00040 #include "nsAETokens.h"
00041 #include "nsAECoreClass.h"
00042 #include "nsAEApplicationClass.h"
00043 
00044 #include "nsAEDocumentClass.h"
00045 
00046 
00047 /*----------------------------------------------------------------------------
00048        AEDocumentClass 
00049        
00050 ----------------------------------------------------------------------------*/
00051 AEDocumentClass::AEDocumentClass()
00052 :      AEGenericClass(cDocument, typeNull)
00053 {
00054 }
00055 
00056 
00057 /*----------------------------------------------------------------------------
00058        ~AEDocumentClass 
00059        
00060 ----------------------------------------------------------------------------*/
00061 AEDocumentClass::~AEDocumentClass()
00062 {
00063 }
00064 
00065 #pragma mark -
00066 
00067 /*----------------------------------------------------------------------------
00068        PropertyFromApplicationAccessor 
00069        
00070 ----------------------------------------------------------------------------*/
00071 pascal OSErr AEDocumentClass::PropertyAccessor(         DescType                    desiredClass,
00072                                                                              const AEDesc*        containerToken,
00073                                                                              DescType                    containerClass,
00074                                                                              DescType                    keyForm,
00075                                                                              const AEDesc*        keyData,
00076                                                                              AEDesc*                     resultToken,
00077                                                                              long                        refCon)
00078 {
00079        AEDocumentClass*     docClass = reinterpret_cast<AEDocumentClass *>(refCon);
00080        if (!docClass) return paramErr;
00081        
00082        OSErr         err = noErr;
00083        
00084        try
00085        {
00086               docClass->GetProperty(desiredClass, containerToken, containerClass, keyForm, keyData, resultToken);
00087        }
00088        catch(OSErr catchErr)
00089        {
00090               err = catchErr;
00091        }
00092        catch(...)
00093        {
00094               err = paramErr;
00095        }
00096        
00097        return err;
00098 }
00099 
00100 /*----------------------------------------------------------------------------
00101        GetItemFromContainer 
00102        
00103        Not appropriate for the application
00104 ----------------------------------------------------------------------------*/
00105 void AEDocumentClass::GetItemFromContainer(             DescType                    desiredClass,
00106                                                                       const AEDesc*        containerToken,
00107                                                                       DescType                    containerClass, 
00108                                                                       DescType                    keyForm,
00109                                                                       const AEDesc*        keyData,
00110                                                                       AEDesc*                     resultToken)
00111 {
00112        ThrowIfOSErr(errAEEventNotHandled);
00113 }
00114 
00115 /*----------------------------------------------------------------------------
00116        GetDocumentFromApp 
00117        
00118 ----------------------------------------------------------------------------*/
00119 void AEDocumentClass::GetDocumentFromApp(        DescType                    desiredClass,        // cDocument
00120                                                                       const AEDesc*        containerToken,      // null container
00121                                                                       DescType                    containerClass,       // cApplication
00122                                                                       DescType                    keyForm,
00123                                                                       const AEDesc*        keyData,
00124                                                                       AEDesc*                     resultToken)         // specified Document is returned in result
00125 {
00126        OSErr                       err                  = noErr;
00127        DescType                           keyDataType   = keyData->descriptorType;
00128        long                               index;
00129        long                               numItems;
00130        Boolean                            wantsAllItems = false;
00131        StAEDesc                           startObject;  // These are used to resolve formRange
00132        StAEDesc                           stopObject;
00133        DocumentReference           document;
00134        Str63                       documentName;
00135        CoreTokenRecord             token;
00136        
00137        numItems = CountDocuments();
00138 
00139        switch (keyForm) 
00140        {
00141               case formName:                                                                             // Document by name
00142                      {
00143                             if (DescToPString(keyData, documentName, 63) != noErr)
00144                             {
00145                                    err = errAECoercionFail;
00146                             }
00147                             else
00148                             {
00149                                    document = GetDocumentByName(documentName);
00150                                    if (document == nil)
00151                                           err = errAENoSuchObject;
00152                             }
00153                      }
00154                      break;
00155               
00156               case formAbsolutePosition:                                                          // Document by number
00157                      err = NormalizeAbsoluteIndex(keyData, &index, numItems, &wantsAllItems);
00158                      if ((err == noErr) && (wantsAllItems == false))
00159                      {
00160                             document = GetDocumentByIndex(index);
00161                             if (document == nil)
00162                                    err = errAEIllegalIndex;
00163                      }
00164                      break; 
00165               
00166               case formRelativePosition:
00167                      ProcessFormRelativePostition(containerToken, keyData, &document);
00168                      break; 
00169               
00170               case formRange:
00171                      switch (keyDataType)
00172                      {             
00173                             case typeRangeDescriptor:
00174                                    err = ProcessFormRange((AEDesc *)keyData, &startObject, &stopObject);
00175                                    if (err == noErr)
00176                                    {
00177                                           ConstAETokenDesc     startTokenDesc(&startObject);
00178                                           ConstAETokenDesc     stopTokenDesc(&startObject);
00179                                           
00180                                           DescType startType = startTokenDesc.GetDispatchClass();
00181                                           DescType stopType  = stopTokenDesc.GetDispatchClass();
00182         
00183                                           if (startType != cDocument || stopType != cDocument)
00184                                                  err = errAEWrongDataType;
00185                                    }
00186                                    break;
00187 
00188                             default:
00189                                    err = errAEWrongDataType;
00190                                    break; 
00191                      }
00192                      break; 
00193 
00194               default:
00195                      err = errAEEventNotHandled;
00196                      break;
00197        }
00198        
00199        // if user asked for all items, and there aren't any,
00200        // we'll be kind and return an empty list.
00201 
00202        if (wantsAllItems && (err == errAENoSuchObject || err == errAEIllegalIndex))
00203        {
00204               err = AECreateList(NULL, 0, false, (AEDescList*)resultToken);
00205               ThrowIfOSErr(err);
00206               return;
00207        }
00208 
00209        ThrowIfOSErr(err);
00210        
00211        // fill in the result token
00212 
00213        token.dispatchClass         = GetClass();
00214        token.objectClass    = GetClass();
00215        token.propertyCode   = typeNull;
00216 
00217        if (wantsAllItems)
00218        {                                         
00219               err = AECreateList(NULL, 0, false, (AEDescList*)resultToken);
00220               
00221               if (err == noErr)
00222               {
00223                      for (index = 1; index <= numItems; index++)
00224                      {
00225                             document = GetDocumentByIndex(index);
00226                             ThrowIfOSErr(errAEEventNotHandled);
00227                             
00228                             token.documentID = GetDocumentID(document);
00229                             
00230                             err = AEPutPtr(resultToken, 0, desiredClass, &token, sizeof(token));
00231                             ThrowIfOSErr(err);
00232                      }
00233               }
00234        }
00235        else if (keyForm == formRange)
00236        {                    
00237               DocumentReference    beginDocument;
00238               DocumentReference    endDocument;
00239               long                        beginIndex;
00240               long                        endIndex;
00241               
00242               beginDocument = GetDocumentReferenceFromToken(&startObject);
00243               beginIndex = GetDocumentIndex(beginDocument);
00244 
00245               endDocument = GetDocumentReferenceFromToken(&stopObject);
00246               endIndex = GetDocumentIndex(endDocument);
00247                                                         
00248               err = AECreateList(NULL, 0, false, (AEDescList*)resultToken);
00249               ThrowIfOSErr(err);
00250                      
00251               if (beginIndex > endIndex) // swap elements
00252               {
00253                      DocumentReference temp;
00254                      temp                 = beginDocument;
00255                      beginDocument = endDocument;
00256                      endDocument   = temp;
00257               }
00258               
00259               document = beginDocument;
00260               while (document != nil)
00261               {
00262                      token.documentID = GetDocumentID(document);
00263                      
00264                      err = AEPutPtr(resultToken, 0, desiredClass, &token, sizeof(token));
00265                      ThrowIfOSErr(err);
00266 
00267                      if (document == endDocument)
00268                             break;
00269                      document = GetNextDocument(document);
00270               }
00271        }
00272        else
00273        {
00274               token.documentID = GetDocumentID(document);
00275               err = AECreateDesc(desiredClass, &token, sizeof(token), resultToken);
00276        }
00277 
00278        ThrowIfOSErr(err);          
00279 }
00280 
00281 
00282 /*----------------------------------------------------------------------------
00283        DocumentAccessor 
00284        
00285        Document from null accessor
00286 ----------------------------------------------------------------------------*/
00287 pascal OSErr AEDocumentClass::DocumentAccessor(         DescType                    desiredClass,        // cDocument
00288                                                                              const AEDesc*        containerToken,      // null container
00289                                                                              DescType                    containerClass,       // cApplication
00290                                                                              DescType                    keyForm,
00291                                                                              const AEDesc*        keyData,
00292                                                                              AEDesc*                     resultToken,         // specified Document is returned in result
00293                                                                              long                        refCon)
00294 {
00295        AEDocumentClass*            docClass = reinterpret_cast<AEDocumentClass *>(refCon);
00296        if (!docClass) return paramErr;
00297        
00298        OSErr         err = noErr;
00299        
00300        try
00301        {
00302               docClass->GetDocumentFromApp(desiredClass, containerToken, containerClass, keyForm, keyData, resultToken);
00303        }
00304        catch(OSErr catchErr)
00305        {
00306               err = catchErr;
00307        }
00308        catch(...)
00309        {
00310               err = paramErr;
00311        }
00312        
00313        return err;
00314 }
00315 
00316                                                                              
00317 
00318 
00319 #pragma mark -
00320 
00321 /*----------------------------------------------------------------------------
00322        ProcessFormRelativePostition 
00323        
00324 ----------------------------------------------------------------------------*/
00325 void AEDocumentClass::ProcessFormRelativePostition(const AEDesc* anchorToken, const AEDesc *keyData, DocumentReference *document)
00326 {
00327        ConstAETokenDesc            tokenDesc(anchorToken);
00328        OSErr                err = noErr;
00329        DescType                    positionEnum;
00330        DocumentReference    anchorDocument;
00331        DocumentReference    relativeDocument = nil;
00332        
00333        *document = nil;
00334        
00335        anchorDocument = GetDocumentReferenceFromToken(anchorToken);
00336 
00337        if (err == noErr)
00338        {
00339        
00340               switch (keyData->descriptorType)
00341               {
00342                  case typeEnumerated:
00343                             if (DescToDescType((AEDesc*)keyData, &positionEnum) != noErr)
00344                             {
00345                                    err = errAECoercionFail;
00346                             }
00347                             else
00348                             {
00349                                    switch (positionEnum)
00350                                    {
00351                                           case kAENext:                                    // get the document behind the anchor
00352                                                  *document = GetPreviousDocument(anchorDocument);
00353                                                  if (*document == nil)
00354                                                         err = errAENoSuchObject;
00355                                                  break;
00356                                                  
00357                                           case kAEPrevious:                                // get the document in front of the anchor
00358                                                  *document = GetNextDocument(anchorDocument);
00359                                                  if (*document == nil)
00360                                                         err = errAENoSuchObject;
00361                                                  break;
00362                                                  
00363                                           default:
00364                                                  err = errAEEventNotHandled;
00365                                                  break;
00366                                    }
00367                             }
00368                             break;
00369                             
00370                      default:
00371                             err = errAECoercionFail;
00372                             break;
00373               }
00374        }
00375        
00376        ThrowIfOSErr(err);
00377 }
00378 
00379 #pragma mark -
00380 
00381 /*----------------------------------------------------------------------------
00382        CanGetProperty 
00383        
00384 ----------------------------------------------------------------------------*/
00385 Boolean AEDocumentClass::CanGetProperty(DescType property)
00386 {
00387        Boolean       result = false;
00388        
00389        switch (property)
00390        {
00391               // Properties we can get:
00392               
00393               case pBestType:
00394               case pClass:
00395               case pDefaultType:
00396               case pObjectType:
00397               
00398               case pName:
00399               case pProperties:
00400               case pIsModified:
00401                      result = true;
00402                      break;
00403                      
00404               // Properties we can't get:
00405               default:
00406                      result = Inherited::CanGetProperty(property);
00407                      break;
00408        }
00409 
00410        return result;
00411 }
00412 
00413 
00414 /*----------------------------------------------------------------------------
00415        CanSetProperty 
00416        
00417 ----------------------------------------------------------------------------*/
00418 Boolean AEDocumentClass::CanSetProperty(DescType property)
00419 {
00420        Boolean       result = false;
00421        
00422        switch (property)
00423        {
00424               // Properties we can set:
00425               
00426               case pName:
00427                      result = true;
00428                      break;
00429                      
00430               // Properties we can't set:
00431 
00432               case pBestType:
00433               case pClass:
00434               case pDefaultType:
00435               case pObjectType:
00436               
00437               case pProperties:
00438               case pIsModified:
00439                      result = false;
00440                      break;
00441                      
00442               default:
00443                      result = Inherited::CanSetProperty(property);
00444                      break;
00445        }
00446 
00447        return result;
00448 }
00449 
00450 
00451 #pragma mark -
00452 
00453 
00454 /*----------------------------------------------------------------------------
00455        GetDataFromObject 
00456        
00457 ----------------------------------------------------------------------------*/
00458 void AEDocumentClass::GetDataFromObject(const AEDesc *token, AEDesc *desiredTypes, AEDesc *data)
00459 {
00460        OSErr                err                         = noErr;
00461        Boolean                     usePropertyCode      = false;      
00462        DocumentReference    document                    = nil;
00463        ConstAETokenDesc            tokenDesc(token);
00464        DescType                    aType                = cDocument;
00465        Str63                documentName;
00466        Boolean                     isModified;
00467        
00468        // ugh
00469        document = GetDocumentReferenceFromToken(token);
00470        ThrowIfOSErr(err);
00471        if (document == nil)
00472               ThrowIfOSErr(paramErr);
00473 
00474        GetDocumentName(document, documentName);                                     
00475        isModified = DocumentIsModified(document);                                          
00476 
00477        usePropertyCode = tokenDesc.UsePropertyCode();
00478        
00479        DescType propertyCode = tokenDesc.GetPropertyCode();
00480        
00481        switch (propertyCode)
00482        {
00483               case pProperties:
00484                      err = AECreateList(NULL, 0L, true, data);
00485                      ThrowIfOSErr(err);
00486 
00487                      err = AEPutKeyPtr(data, pObjectType,      typeType,     &aType,                     sizeof(DescType));
00488                      err = AEPutKeyPtr(data, pName,            typeChar,     &documentName[1],    documentName[0]);
00489                      err = AEPutKeyPtr(data, pIsModified,      typeBoolean,  &isModified,         sizeof(Boolean));
00490                      break;
00491                      
00492               case pBestType:
00493               case pClass:
00494               case pDefaultType:
00495               case pObjectType:
00496                      err = AECreateDesc(typeType, &aType, sizeof(DescType), data);
00497                      break;
00498                      
00499               case pName:
00500                      err = AECreateDesc(typeChar, &documentName[1], documentName[0], data);
00501                      break;
00502                      
00503               case pIsModified:
00504                      err = AECreateDesc(typeBoolean, &isModified, sizeof(Boolean), data);
00505                      break;
00506                      
00507               default:
00508                      Inherited::GetDataFromObject(token, desiredTypes, data);
00509                      break;
00510        }
00511        
00512        ThrowIfOSErr(err);
00513 }
00514 
00515 /*----------------------------------------------------------------------------
00516        SetDataForObject 
00517        
00518 ----------------------------------------------------------------------------*/
00519 void AEDocumentClass::SetDataForObject(const AEDesc *token, AEDesc *data)
00520 {
00521        OSErr                       err = noErr;         
00522        Boolean                            usePropertyCode;
00523        DescType                           propertyCode;
00524        DocumentReference           document = nil;
00525        ConstAETokenDesc                   tokenDesc(token);
00526        StAEDesc                           propertyRecord;
00527 
00528        usePropertyCode = tokenDesc.UsePropertyCode();
00529        document = GetDocumentReferenceFromToken(token);
00530               
00531        if (usePropertyCode == false)
00532        {
00533               err = errAEWriteDenied;
00534        }
00535        else
00536        {
00537               propertyCode = tokenDesc.GetPropertyCode();
00538               
00539               if (data->descriptorType == typeAERecord)
00540               {             
00541                      SetDocumentProperties(document, data);
00542               }
00543               else   // Build a record with one property
00544               {
00545                      err = AECreateList(NULL, 0L, true, &propertyRecord);
00546                      ThrowIfOSErr(err);
00547                      
00548                      err = AEPutKeyDesc(&propertyRecord, propertyCode, data);
00549                      ThrowIfOSErr(err);
00550               
00551                      SetDocumentProperties(document, &propertyRecord);
00552               }
00553        }
00554 
00555        ThrowIfOSErr(err);
00556 }
00557 
00558 
00559 /*----------------------------------------------------------------------------
00560        SetDocumentProperties 
00561        
00562 ----------------------------------------------------------------------------*/
00563 void   AEDocumentClass::SetDocumentProperties(DocumentReference document, AEDesc *propertyRecord)
00564 {
00565 
00566 }
00567 
00568 
00569 /*----------------------------------------------------------------------------
00570        CountDocuments 
00571        
00572 ----------------------------------------------------------------------------*/
00573 long AEDocumentClass::CountDocuments()
00574 {
00575        return 0;
00576 }
00577 
00578 
00579 /*----------------------------------------------------------------------------
00580        GetDocumentByName 
00581        
00582 ----------------------------------------------------------------------------*/
00583 DocumentReference  AEDocumentClass::GetDocumentByName(ConstStr255Param docName)
00584 {
00585        return nil;
00586 }
00587 
00588 /*----------------------------------------------------------------------------
00589        GetDocumentByIndex 
00590        
00591 ----------------------------------------------------------------------------*/
00592 DocumentReference  AEDocumentClass::GetDocumentByIndex(long index)
00593 {
00594        return nil;
00595 }
00596 
00597 /*----------------------------------------------------------------------------
00598        GetDocumentByID 
00599        
00600 ----------------------------------------------------------------------------*/
00601 DocumentReference AEDocumentClass::GetDocumentByID(long docID)
00602 {
00603        return nil;
00604 }
00605 
00606 /*----------------------------------------------------------------------------
00607        GetNextDocument 
00608        
00609 ----------------------------------------------------------------------------*/
00610 DocumentReference AEDocumentClass::GetNextDocument(DocumentReference docRef)
00611 {
00612        return nil;
00613 }
00614 
00615 
00616 /*----------------------------------------------------------------------------
00617        GetPreviousDocument 
00618        
00619 ----------------------------------------------------------------------------*/
00620 DocumentReference AEDocumentClass::GetPreviousDocument(DocumentReference docRef)
00621 {
00622        return nil;
00623 }
00624 
00625 
00626 /*----------------------------------------------------------------------------
00627        GetDocumentReferenceFromToken 
00628        
00629 ----------------------------------------------------------------------------*/
00630 DocumentReference AEDocumentClass::GetDocumentReferenceFromToken(const AEDesc *token)
00631 {
00632        ConstAETokenDesc     tokenDesc(token);
00633        long                 docID = tokenDesc.GetDocumentID();
00634        
00635        return GetDocumentByID(docID);
00636 }
00637 
00638 /*----------------------------------------------------------------------------
00639        CloseWindowSaving 
00640        
00641 ----------------------------------------------------------------------------*/
00642 void AEDocumentClass::CloseWindowSaving(AEDesc *token, const AEDesc *saving, AEDesc *savingIn)
00643 {
00644        OSErr                err                  = noErr;
00645        DocumentReference    document;
00646 
00647        document = GetDocumentReferenceFromToken(token);
00648        
00649        if (document != nil)
00650        {
00651               // DestroyDocument(document);
00652        }
00653        else
00654               err = errAEEventNotHandled;
00655        
00656        ThrowIfOSErr(err);
00657 }
00658 
00659 #pragma mark -
00660 
00661 /*----------------------------------------------------------------------------
00662        CreateSelfSpecifier 
00663 
00664 ----------------------------------------------------------------------------*/
00665 void AEDocumentClass::CreateSelfSpecifier(const AEDesc *token, AEDesc *outSpecifier)
00666 {
00667        ThrowIfOSErr(errAENoSuchObject);
00668 }
00669 
00670 #pragma mark -
00671 
00672 /*----------------------------------------------------------------------------
00673        GetDocumentID 
00674        
00675 ----------------------------------------------------------------------------*/
00676 long GetDocumentID(DocumentReference docRef)
00677 {
00678        return 0;
00679 }
00680 
00681        
00682 /*----------------------------------------------------------------------------
00683        GetDocumentIndex 
00684        
00685 ----------------------------------------------------------------------------*/
00686 long GetDocumentIndex(DocumentReference docRef)
00687 {
00688        return 0;
00689 }
00690 
00691 /*----------------------------------------------------------------------------
00692        GetDocumentIndex 
00693        
00694 ----------------------------------------------------------------------------*/
00695 void   GetDocumentName(DocumentReference docRef, Str63 docName)
00696 {
00697        docName[0] = 0;
00698 }
00699 
00700 
00701 /*----------------------------------------------------------------------------
00702        GetDocumentIndex 
00703        
00704 ----------------------------------------------------------------------------*/
00705 Boolean DocumentIsModified(DocumentReference docRef)
00706 {
00707        return false;
00708 }
00709