sgscanf.cpp

Go to the documentation of this file.
00001 // $Id: sgscanf.cpp 1282 2006-06-09 09:46:49Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 // Gallery library FILE SCANNING class
00099 
00100 /*
00101 */
00102 
00103 #include "camtypes.h"
00104 #include "sgscanf.h"
00105 
00106 //#include "sgscan.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00107 //#include "ccfile.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00108 //#include "pathname.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00109 //#include "thumb.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00110 //#include "resource.h"
00111 //#include "richard.h"
00112 //#include "fixmem.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00113 //#include "app.h"      // for pref stuff... - in camtypes.h [AUTOMATICALLY REMOVED]
00114 #include "progress.h"
00115 //#include "sgallery.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00116 #include "sglib.h"
00117 //#include "sgtree.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00118 #include "sgfonts.h"
00119 #include "sglcart.h"
00120 #include "sglfills.h"
00121 #include "sgindgen.h"
00122 #include "sgliboil.h"
00123 #include "sglbase.h"
00124 #include "product.h"
00125 //#include "richard2.h"
00126 
00127 #include "mfccopy.h"
00128 
00129 DECLARE_SOURCE("$Revision: 1282 $");
00130 
00131 CC_IMPLEMENT_DYNCREATE(LibraryFile, ListItem)
00132 
00133 #define new CAM_DEBUG_NEW
00134 
00135 Library *LibraryFile::FirstLibraryAdded = NULL;     // Two statics for redraw/scroll purposes...
00136 Library *LibraryFile::LastLibraryAdded = NULL;
00137 
00138 /***********************************************************************************************
00139 
00140 >   LibraryFile::LibraryFile()
00141 
00142     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00143     Created:    18/2/95
00144 
00145     Purpose:    Constructor
00146 
00147 ***********************************************************************************************/
00148 
00149 LibraryFile::LibraryFile()
00150 {
00151     ParentGallery = NULL;
00152     ParentLibraryGallery = NULL;
00153     MyType = SGLib_Blank;
00154     InitScrollRedrawSystem();
00155 }
00156 
00157 
00158 
00159 /***********************************************************************************************
00160 
00161 >   LibraryFile::~LibraryFile()
00162 
00163     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00164     Created:    18/2/95
00165 
00166     Purpose:    Destructor
00167 
00168 ***********************************************************************************************/
00169 
00170 LibraryFile::~LibraryFile()
00171 {
00172     // Delete any live libraries. They will inform the gallery as they die
00173     if (!Libraries.IsEmpty())
00174         Libraries.DeleteAll();
00175 
00176     TRACEUSER( "Richard", _T("~LibraryFile called\n"));
00177 }
00178 
00179 
00180 
00181 /***********************************************************************************************
00182 
00183 >   INT32 LibraryFile::Init(SuperGallery *ParentGal, PathName *Path, SGLibType Type,
00184                                                      BOOL Updated = FALSE, BOOL DoScroll = TRUE)
00185 
00186     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00187     Created:    18/2/95
00188 
00189     Inputs:     ParentGal   - points to the parent super gallery to whack the new groups in.
00190                 Path        - should point to the library itself, "C:\Testlib", etc...
00191                 Type        - the type of library we're interested in creating
00192                 Updated     - true if the group has just been updated prior to calling this init function
00193                 DoScroll    - true if you want this function to do the scroll / redraw... Otherwise it
00194                               is up to the caller to do it - note that you can call the DoScrollRedraw
00195                               function after calling the Init function, so if groups are to be shuffled
00196                               about afterwards, that would be the way to go...
00197 
00198     Returns:    The number of Libraries (display groups) created (not items created!)
00199 
00200     Purpose:    The main entry point for the galleries...
00201                 Locates and runs through each entry in the main index file, if the
00202                 entry matches the given type, we go and create a group for the sub-lib,
00203                 and add all the items in the sub-lib to it...
00204                 Returns number of groups created (0 indicates a possible problem)
00205 
00206     Notes:      This builds a list of available libraries, which is stored for future
00207                 reference.
00208 
00209                 This function needs splitting and tidying up sometime...
00210 
00211     SeeAlso:    LibraryFile::GetLibraryList
00212 
00213 ***********************************************************************************************/
00214 
00215 INT32 LibraryFile::Init(SuperGallery *ParentGal, PathName *APath, SGLibType Type, BOOL Updated, BOOL DoScroll)
00216 {
00217 #ifndef EXCLUDE_GALS
00218     if(ParentGal == NULL || APath == NULL || !Libraries.IsEmpty())
00219     {
00220         ERROR3("LibraryFile::Init - NULL parameters are illegal OR Init called > 1 times");
00221         if(!Libraries.IsEmpty())
00222             return(Libraries.GetCount());
00223         else
00224             return 0;
00225     }
00226 
00227     BOOL ok = TRUE;
00228 
00229     // Tidy up Path a bit
00230     String_256 OurPath(APath->GetPath());
00231     LibraryFile::TidyUpSubPath(&OurPath);
00232 
00233     // Now point Path to the new pathname
00234     PathName ModifiedPath(OurPath);
00235     PathName *Path = &ModifiedPath;
00236 
00237     if(!ModifiedPath.IsValid())
00238     {
00239         ERROR3("LibraryFile::Init -> Modified library path is invalid");
00240         return 0;
00241     }
00242 
00243     // Remember the pathname and type
00244     MyPath = *Path;
00245     MyType = Type;
00246 
00247     ParentGallery = ParentGal;
00248     if(ParentGallery->IsKindOf(CC_RUNTIME_CLASS(LibraryGallery)))
00249         ParentLibraryGallery = (LibraryGallery *)ParentGal;
00250     else
00251     {
00252         ERROR3("LibraryFile::Init passed a non-library gallery - yikes...");
00253         return 0;
00254     }
00255 
00256     // Need to reset the Quiet status before a stream of Library::Init calls
00257     ParentLibraryGallery->SetQuietStatus(FALSE);
00258 
00259     BOOL Retry = TRUE;
00260     while(Retry)
00261     {
00262         Retry = FALSE;
00263     
00264         // Would be nice to have a way of adding a file to a path in PathName... Is there one ?
00265         if(!SGLibOil::FileExists(Path))
00266         {
00267             // We're opening the font gallery, but can't find the font library path - don't warn
00268             if(Type == SGLib_Font)
00269                 return 0;
00270 
00271             // tell the user that the directory doesn't exist
00272             String_256 WarnMsg;
00273             String_256 DefaultIndex;
00274             String_256 IndexDesc;
00275             BOOL CanGenerate;
00276         
00277             ok = LibraryFile::GetSubIndexDetails(ParentLibraryGallery, &DefaultIndex, &IndexDesc, &CanGenerate);
00278 
00279             String_256 TmpPath(Path->GetLocation(FALSE));
00280             LibraryFile::TidyUpSubPath(&TmpPath);
00281 
00282             // Taken out by Graham 30/10/97: If the gallery had no directory specified,
00283             //we used to throw a warning which said "do you want to specify another folder?"
00284             //We don't do this any more, because the default is to open all galleries empty and
00285             //then download stuff from the Xara web site
00286 #if 0 
00287             WarnMsg.MakeMsg(_R(IDS_BROWSE_OR_SCAN), (TCHAR *)IndexDesc, (TCHAR *)TmpPath);
00288             Error::SetError(0, WarnMsg, 0);
00289             INT32 ButtonPressed = InformWarning(0, _R(IDS_BROWSE), _R(IDS_RETRY), _R(IDS_CANCEL)/*, _R(IDS_HELP)*/);
00290 #else   // WEBSTER
00291             INT32 ButtonPressed = 3;
00292 #endif  // WEBSTER
00293             TRACEUSER( "Richard", _T("ButtonPressed: %d\n"), ButtonPressed);
00294             Error::ClearError();
00295             switch(ButtonPressed)
00296             {
00297                 case 1:
00298                 {
00299                     // Open the Browse dialog (or the Add.. dialog as it seems to be called now)
00300                     PathName ThePath(*Path);
00301                 
00302                     // This returns FALSE if Cancel was hit, or an error occurred.
00303                     if(!SGLibOil::GetLibPath(ParentLibraryGallery, &ThePath, CanGenerate, Type))
00304                     {
00305                         ERROR3("GetLibPath returned FALSE in LF::Init");
00306                         return 0;
00307                     }
00308                     else
00309                     {
00310                         ModifiedPath = ThePath;
00311                         if(!ModifiedPath.IsValid())
00312                         {
00313                             ERROR3("LibraryFile::Init -> scanned library path is invalid");
00314                             return 0;
00315                         }
00316 
00317                         // Remember the pathname
00318                         MyPath = ThePath;
00319 
00320                         switch(Type)
00321                         {
00322                             case SGLib_ClipArt:
00323                             case SGLib_Bitmap:
00324                                 LibClipartSGallery::DefaultLibraryPath = MyPath.GetPath();
00325                                 LibClipartSGallery::ClipartPath = LibClipartSGallery::DefaultLibraryPath;
00326                                 break;
00327 
00328                             case SGLib_ClipArt_WebThemes:
00329                                 LibClipartSGallery::DefaultLibraryPath = MyPath.GetPath();
00330                                 LibClipartSGallery::WebThemePath = LibClipartSGallery::DefaultLibraryPath;
00331                                 break;
00332 
00333 #ifndef STANDALONE
00334                             case SGLib_Texture:
00335                             case SGLib_Fractal:
00336                                 LibFillsSGallery::DefaultLibraryPath = MyPath.GetPath();
00337                                 break;
00338 
00339                             case SGLib_Font:
00340                                 // WEBSTER-Martin-09/01/97 - Put back by Ranbir.
00341                                 //#ifndef WEBSTER
00342                                 FontsSGallery::DefaultLibraryPath = MyPath.GetPath();
00343                                 break; // Not in webster so we get the error below
00344                                 //#endif // WEBSTER
00345 #endif
00346                             default:
00347                                 ERROR2(FALSE,"Library::ScanForLocation Type not present!");
00348                                 break;
00349                         }
00350                     }
00351                     break;
00352                 }                       
00353 
00354                 case 2:
00355                     Retry = TRUE;
00356 #if 0
00357                     {
00358                         // Scan
00359                         String_256 Result;
00360                         if(!Library::ScanForLocation(Type, &Result))
00361                         {
00362                             ERROR3("No libraries found...");
00363                             return 0;
00364                         }
00365 
00366                         if(!ModifiedPath.SetPathName(Result))
00367                         {
00368                             ERROR3("LibraryFile::Init -> scanned library path is invalid");
00369                             return 0;
00370                         }
00371 
00372                         // Remember the pathname and type
00373                         MyPath = *Path;
00374                     }
00375 #endif
00376                     break;
00377 
00378                 case 3:
00379                     // Cancel
00380                     return 0;
00381             }
00382         }
00383     }
00384 
00385     // Wipe libraries added to gallery for scroll / redraw purposes...
00386     InitScrollRedrawSystem();
00387 
00388     // Check the actual path exists
00389     if(SGLibOil::FileExists(Path))
00390     {
00391         // Would be nice to have a way of adding a file to a path in PathName... Is there one ?
00392         String_256 IndexFile((const TCHAR *)Path->GetPath(TRUE)); // "%s\\XaraInfo\\index.txt"
00393         IndexFile += String_16(_R(IDS_LIBRARIES_XARAINFO_DIRNAME));
00394         IndexFile += TEXT("\\") + String_16(_R(IDS_LIBRARIES_INDEX_FILENAME));
00395 
00396         PathName IndexFilePath(IndexFile);
00397         if(!IndexFilePath.IsValid())
00398         {
00399             ERROR3("LibraryFile::Init indexfilepath is invalid");
00400             return 0;
00401         }
00402 
00403         CCDiskFile MainIndex;
00404         if (!MainIndex.InitLexer(FALSE))
00405         {
00406             // SetError!
00407             ERROR3("LibraryFile::LibraryFile InitLexer failed");
00408             return(0);
00409         }
00410 
00411         if(SGLibOil::FileExists(&IndexFilePath))
00412         {
00413             // Count lines in index file
00414             INT32 Count = CountLines(&IndexFilePath);
00415 
00416             TRACEUSER( "Richard", _T("%d lines in index file\n"), Count);
00417 
00418             // Used for the percentage display
00419             INT32 CurrentGroupNumber = 0;
00420 
00421             // Just in case there's a slow job already going on...
00422             SmashSlowJob();
00423             String_64 SlowJob(_R(IDS_LIBRARY_SCANNING));
00424             BeginSlowJob(Count, FALSE, &SlowJob);
00425         
00426             // Now use the index file to create each group in turn
00427             if (MainIndex.open(IndexFilePath, ios::in))
00428             {
00429                 MainIndex.SetWhitespace("");        // Setting this to blank lets us read non-"'d strings
00430                 MainIndex.SetDelimiters(",");       // ,s delimit our fields
00431                 MainIndex.SetCommentMarker('#');    // #'d lines are commented out
00432                 MainIndex.SetStringDelimiters("");  // No string delimiters
00433 
00434                 String_64 Directory;
00435                 String_64 Description;
00436                 String_64 SubIndex;
00437                 String_64 Kind;
00438                 LexTokenType TT;
00439     
00440                 BOOL EscapePressed = FALSE;
00441 
00442                 while(ok && !EscapePressed)
00443                 {
00444                     if(!MainIndex.GetToken()) break;        // Get SubLib directory name
00445 
00446                     // Keep reading tokens until we hit a normal one... (skips line ends and
00447                     // comments for us
00448                     TT = MainIndex.GetTokenType();      
00449                     while (TT != TOKEN_NORMAL && ok)
00450                     {
00451                         ok = MainIndex.GetToken();
00452                         if(!ok) break;
00453                         TT = MainIndex.GetTokenType();      
00454                         ok = (TT != TOKEN_EOF);
00455                         if(!ok) break;
00456                     }
00457                     if(!ok) break;
00458     
00459                     Directory = MainIndex.GetTokenBuf();
00460                     KillLeadingSpaces(&Directory);
00461 
00462                     if(!MainIndex.GetToken()) break;        // Get ','
00463                     if(!MainIndex.GetToken()) break;        // Get Description
00464                     String_256 Description256;
00465                     Description256 = MainIndex.GetTokenBuf();
00466                     KillLeadingSpaces(&Description256);
00467                     Description256.Left(&Description, 60);
00468 
00469                     if(!MainIndex.GetToken()) break;        // Get ','
00470                     if(!MainIndex.GetToken()) break;        // Get Sub Library Index name
00471                     SubIndex = MainIndex.GetTokenBuf();
00472                     KillLeadingSpaces(&SubIndex);
00473 
00474                     if(!MainIndex.GetToken()) break;        // Get ','
00475                     if(!MainIndex.GetToken()) break;        // Get type of files in sublib
00476                     Kind = MainIndex.GetTokenBuf();
00477                     KillLeadingSpaces(&Kind);
00478     
00479                     BOOL Match = FALSE;
00480                     Match = ParentLibraryGallery->CheckForIndexMatch(&Kind);
00481 
00482                     if(Match)
00483                     {               
00484                         // Show status of additions
00485                         EscapePressed = !ContinueSlowJob(CurrentGroupNumber++);
00486                 
00487                         // Sort pathname of sublib directory out    
00488                         String_256 SubP(Path->GetPath(TRUE));
00489                         SubP += Directory;
00490                         PathName SubPath(SubP);
00491                         if(!SubPath.IsValid())
00492                         {
00493                             ERROR3("LibraryFile::Init - invalid subpath");
00494                             if(MainIndex.isOpen())
00495                                 MainIndex.close();
00496                             EndSlowJob();                               
00497                             return 0;
00498                         }
00499                                                                    
00500                         // Go ahead and add the new group
00501                         if(ok)
00502                         {
00503                             // Create the sub lib
00504                             Library *NewSubLib = new Library;
00505 
00506                             if (NewSubLib != NULL)
00507                             {
00508                                 // Create the new group in the gallery (note the TRUE for create a virtualised one if
00509                                 // we can to save time / memory)
00510                                 if(NewSubLib->Init(ParentGal, &SubPath, &Description, &SubIndex, Type, Updated, TRUE))
00511                                 {
00512                                     Libraries.AddTail(NewSubLib);
00513 
00514                                     // Keep track of libraries added for redraw purposes...
00515                                     AddNewFolderToScrollRedrawSystem(NewSubLib);
00516                                 }
00517                                 else
00518                                 {
00519                                     // This check is new, should be ok...
00520                                     delete NewSubLib;
00521                                     NewSubLib = NULL;
00522                                     ERROR3("Library::Init failed in LibraryFile::Init");
00523                                     ok = FALSE;
00524                                 }
00525                             }
00526                         }
00527                     }
00528                 }
00529 
00530             } else {
00531                 // Failed to open the index file...
00532 
00533                 // SetError?!
00534                 ERROR3("LibraryFile::LibraryFile couldn't open index file");
00535                 ok = FALSE;
00536             }
00537 
00538             EndSlowJob();                               
00539 
00540         } else {
00541             // The directory given had no XaraInfo\index.txt file, maybe it's a sublib, check
00542             // For defaults...
00543             ok = CheckForSubIndexes(ParentGal, Path, Type, Updated);
00544         }
00545 
00546         // reclaim lexer-buffer memory
00547         MainIndex.DeinitLexer();
00548 
00549         // And close the file
00550         if(MainIndex.isOpen())
00551             MainIndex.close();
00552 
00553         // Scroll / redraw the newly added groups...
00554         if(DoScroll)
00555             DoScrollRedraw();
00556     }
00557     else
00558     {
00559         TRACEUSER( "Richard", _T("Path doesn't exist\n"));
00560     }
00561 
00562     // And return the number of items created
00563     return(Libraries.GetCount());
00564 #endif
00565     return 0;
00566 }
00567 
00568 
00569 
00570 /***********************************************************************************************
00571 
00572 >   static BOOL LibraryFile::GetSubIndexDetails(LibraryGallery *LG,
00573                          String_256 *DefaultIndex, String_256 *IndexDesc, BOOL *CanGenerate)
00574 
00575     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00576     Created:    12/4/95
00577 
00578     Inputs:     LG - LibraryGallery for the LibraryFile
00579     Outputs:    DefaultIndex - default index file name - "XaraClip.txt"
00580                 IndexDesc - Description of gallery - "Clipart"
00581                 CanGenerate - Do we have index generation facilities for this type of library
00582     Returns:    TRUE if things went ok, otherwise FALSE.
00583     Purpose:    Return various library specific details.
00584 
00585 ***********************************************************************************************/
00586 
00587 BOOL LibraryFile::GetSubIndexDetails(LibraryGallery *LG, String_256 *DefaultIndex,
00588                                                         String_256 *IndexDesc, BOOL *CanGenerate)
00589 {
00590     if(DefaultIndex == NULL || IndexDesc == NULL || CanGenerate == NULL || LG == NULL)
00591     {
00592         ERROR3("LibraryFile::GetSubIndexDetails given null params");
00593         return FALSE;
00594     }
00595 
00596     if(LG == NULL)
00597     {
00598         ERROR3("LibraryFile::GetSubIndexDetails called when there was a NULL ParentLibraryGallery");
00599         *CanGenerate = FALSE;
00600     }
00601     else
00602     {
00603         // Get pathname of default sublib index and find out whether we can generate indexes, etc
00604         *CanGenerate = LG->CanCreateIndexes();
00605         return(LG->GetDefaults(DefaultIndex, IndexDesc, NULL));
00606     }
00607 
00608     return FALSE;
00609 }
00610 
00611 
00612 
00613 /***********************************************************************************************
00614 
00615 >   BOOL LibraryFile::CheckForSubIndexes(SuperGallery *ParentGal, PathName *Path, SGLibType Type);
00616 
00617     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00618     Created:    18/2/95
00619 
00620     Inputs:     ParentGal - points to the parent super gallery to whack the new groups in.
00621                 Path - should point to the sublibrary, "C:\Testlib\NewABC", etc...
00622                 Type - the type of library we're interested in creating
00623 
00624     Returns:    TRUE if we did our stuff...
00625 
00626     Purpose:    Checks for default sublibrary indexes. If there are non, and we have
00627                 the technology, give the user the option to create one, and then use that.
00628 
00629                 After all that, add the stuff in the index to the gallery as a group with a
00630                 pathname in its title bar.
00631     Notes:
00632     SeeAlso:
00633 
00634 ***********************************************************************************************/
00635 
00636 BOOL LibraryFile::CheckForSubIndexes(SuperGallery *ParentGal, PathName *Path, SGLibType Type, BOOL Updated)
00637 {
00638     // The directory given had no XaraInfo\index.txt file, maybe it's a sublib, check
00639     // For defaults...
00640 
00641     String_256 DefaultIndex;
00642     String_256 IndexDesc;
00643     BOOL CanGenerate;
00644     BOOL ok = TRUE;
00645 
00646     // Need to reset the Quiet status before a stream of Library::Init calls
00647     ParentLibraryGallery->SetQuietStatus(FALSE);
00648 
00649     if(GetSubIndexDetails(ParentLibraryGallery, &DefaultIndex, &IndexDesc, &CanGenerate))
00650     {
00651         String_256 SubP(Path->GetPath(TRUE)); // "%s\\XaraInfo\\%s"
00652         SubP += String_16(_R(IDS_LIBRARIES_XARAINFO_DIRNAME));
00653         SubP += TEXT("\\") + DefaultIndex;
00654         PathName SubPath(SubP);
00655         if(!SubPath.IsValid())
00656         {
00657             ERROR3("LibraryFile::CheckForSubIndexes invalid subpath");
00658             return FALSE;
00659         }
00660 
00661         BOOL Generate = FALSE;
00662         BOOL Found = FALSE;
00663 
00664         // Is there a default sub index ?
00665         Found = SGLibOil::FileExists(&SubPath);
00666         
00667         if(!Found && CanGenerate)
00668         {
00669             if(Library::RemoteIndexes && GenerateIndexFile::IsDirectoryReadOnly(Path))
00670             {
00671                 // Check whether there's a 'temporary' index for this directory, and 
00672                 // possibly use that instead of the read only directory...
00673 
00674                 String_256 RemoteLocationOfIndex;
00675                 BOOL Existing = GenerateIndexFile::CheckForRemote(Path, &RemoteLocationOfIndex);
00676 
00677                 if(Existing)
00678                 {
00679                     String_256 SubP(RemoteLocationOfIndex); // %s\\XaraInfo\\%s
00680                     SGLibOil::AppendSlashIfNotPresent(&SubP);
00681                     SubP += String_16(_R(IDS_LIBRARIES_XARAINFO_DIRNAME));
00682                     SubP += TEXT("\\") + DefaultIndex;
00683                     PathName TmpSubPath(SubP);
00684                     
00685                     Found = SGLibOil::FileExists(&TmpSubPath);
00686 
00687                     // OK, so there's a remote index sitting pretty in the user's temporary
00688                     // location... Use that and don't bother generating a new one...
00689                     if(Found)
00690                     {
00691                         SubPath.SetPathName(SubP);
00692                         Path->SetPathName(RemoteLocationOfIndex);
00693                     }
00694                 }
00695             }
00696 
00697             if(!Found)
00698             {
00699                 // tell the user that there is no index file, and ask if they want one generating
00700                 String_256 WarnMsg;
00701                 String_256 TmpPath(SubPath.GetLocation(FALSE));
00702                 LibraryFile::TidyUpSubPath(&TmpPath);
00703                 WarnMsg.MakeMsg(_R(IDS_LIBRARY_NO_INDEX_FILE_GEN), (TCHAR *)IndexDesc, (TCHAR *)TmpPath);
00704                 Error::SetError(0, WarnMsg, 0);
00705                 //INT32 ButtonPressed = InformMessage(0, _R(IDS_NOTHUMBNAILS), _R(IDS_THUMBNAILS), _R(IDS_CANCEL)/*, _R(IDS_HELP)*/);
00706                 INT32 ButtonPressed = InformMessage(0, _R(IDS_CREATE), _R(IDS_CANCEL));
00707                 Error::ClearError();
00708 
00709                 if(ButtonPressed < 2)
00710                 {
00711                     // Generate an index...
00712                     String_64 Author(PRODUCT_NAME);
00713                     Generate = GenerateDefaultIndex(&SubPath, Path, &Author, Type, TRUE);
00714                     ok = TRUE;
00715                 }
00716                 else
00717                 {
00718                     // Cancel or help clicked
00719                     ok = FALSE;
00720                 }
00721             }
00722         }
00723 
00724         if(!Found && !CanGenerate)
00725         {
00726             // tell the user that there is no index file, and give them a cancel...
00727             String_256 WarnMsg;
00728             String_256 TmpPath(SubPath.GetLocation(FALSE));
00729             LibraryFile::TidyUpSubPath(&TmpPath);
00730             WarnMsg.MakeMsg(_R(IDS_LIBRARY_NO_INDEX_FILE), (TCHAR *)IndexDesc, (TCHAR *)TmpPath);
00731             Error::SetError(0, WarnMsg, 0);
00732             INT32 ButtonPressed = InformWarning(0, _R(IDS_CANCEL), NULL);
00733             Error::ClearError();
00734             ok = FALSE;
00735         }
00736 
00737         // Check again...
00738         Found = SGLibOil::FileExists(&SubPath);
00739 
00740         if((Found && ok) || (!Found && Generate && ok))
00741         {
00742             String_256 Description256 = Path->GetPath();
00743             AbbreviateName(Description256, 60, TRUE);
00744 
00745             String_64 Description(Description256);
00746             BOOL DoAgain = TRUE;
00747             while (DoAgain)
00748             {
00749                 DoAgain = FALSE;
00750 
00751                 // Create the sub lib
00752                 Library *NewSubLib = new Library;
00753 
00754                 if (NewSubLib != NULL)
00755                 {
00756                     String_64 DefIndex64(DefaultIndex);
00757                     String_256 PathToAdd256 = SubPath.GetLocation(FALSE);
00758                     TidyUpSubPath(&PathToAdd256);
00759                     PathName PathToAdd(PathToAdd256);
00760                     Error::ClearError();
00761 
00762                     // Create the actual group itself
00763                     if(NewSubLib->Init(ParentGal, &PathToAdd, &Description, &DefIndex64, Type, Updated))
00764                     {
00765                         Libraries.AddTail(NewSubLib);
00766 
00767                         ok = TRUE;
00768 
00769                         // Keep track of libraries added for redraw purposes...
00770                         AddNewFolderToScrollRedrawSystem(NewSubLib);
00771                     }
00772                     else
00773                     {
00774                         delete NewSubLib;
00775                         NewSubLib = NULL;
00776 
00777                         String_256 WarnMsg;
00778                         String_256 SmallPath;                   
00779                         PathToAdd256.Left(&SmallPath, 150);
00780                         
00781                         // "The index for '%s' seems to be invalid and requires updating."                      
00782                         WarnMsg.MakeMsg(_R(IDS_LIBRARY_DODGY_INDEX), (TCHAR *)SmallPath);
00783                         Error::SetError(0, WarnMsg, 0);
00784                         INT32 Button = InformWarning(0, _R(IDS_GENERATE), _R(IDS_CANCEL));
00785                         Error::ClearError();
00786 
00787                         String_256 IPathToAdd256(PathToAdd256);
00788                         SGLibOil::AppendSlashIfNotPresent(&IPathToAdd256);
00789                         IPathToAdd256 += TEXT("indexfle.txt");  // This is just to check we can write ok...
00790                         PathName IPathToAdd(IPathToAdd256);
00791 
00792                         if(Button == 1)
00793                             DoAgain = SGLibOil::GenerateClicked(ParentLibraryGallery, &IPathToAdd);
00794                     
00795                         if(!DoAgain)
00796                             ok = FALSE;
00797 
00798                         Error::ClearError();
00799                     }
00800                 }
00801             }
00802         }
00803     }
00804     return ok;
00805 }
00806 
00807 /***********************************************************************************************
00808 
00809 >   BOOL LibraryFile::AddSpecificIndex(SuperGallery *ParentGal, PathName *Path, SGLibType Type,
00810         StringBase *Description = NULL);
00811 
00812     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00813     Created:    28/5/95
00814 
00815     Inputs:     ParentGal   - points to the parent super gallery to whack the new groups in.
00816                 Path        - should point to the index file, "C:\Testlib\NewABC", etc...
00817                 Type        - the type of library we're interested in creating
00818                 Description - Description to whack in the group title (NULL pops the path in)
00819 
00820     Returns:    TRUE if we found and added the index
00821 
00822     Purpose:    Adds a specific index to the gallery
00823 
00824     Notes:      This will not give the option of retrying or relocating if the index wasn't
00825                 found, it'll simply return FALSE;
00826 
00827                 This function can be used in preference to Init
00828 
00829 ***********************************************************************************************/
00830 
00831 BOOL LibraryFile::AddSpecificIndex(SuperGallery *ParentGal, PathName *Path, SGLibType Type,
00832     StringBase *Description, UINT32 bModified, BOOL bIsWebLib)
00833 {
00834     if(ParentGal == NULL || Path == NULL || !Libraries.IsEmpty() || !Path->IsValid())
00835     {
00836         ERROR3("LibraryFile::AddSpecificIndex - Problems on entry");
00837         return FALSE;
00838     }
00839 
00840     // Check the path for the library exists
00841 //  if(!SGLibOil::FileExists(Path))
00842 //      return FALSE;
00843 
00844     // Remember the pathname and type
00845     MyPath = *Path;
00846     MyType = Type;
00847     ParentGallery = ParentGal;
00848 
00849     // Create the sub lib
00850     Library *NewSubLib = new Library;
00851 
00852     if (NewSubLib != NULL)
00853     {
00854         // Path = "C:\testlib\animals\xarainfo\animals.txt"
00855         // Path of files themselves... FilesPath = "C:\testlib\animals"
00856         NewSubLib->m_bIsWebLib = bIsWebLib;
00857         PathName TmpPath(Path->GetLocation(FALSE));
00858         NewSubLib->SetModified(bModified);
00859 
00860         // If it's a root directory we need the slash...
00861         PathName FilesPath;
00862         String_256 TmpRootCheck(TmpPath.GetLocation(TRUE));
00863         if(SGLibOil::IsRootDirectory(&TmpRootCheck))
00864             FilesPath.SetPathName(TmpPath.GetLocation(TRUE));
00865         else
00866             FilesPath.SetPathName(TmpPath.GetLocation(FALSE));
00867 
00868         // Default title for the new group
00869         String_256 Desc256;
00870         String_64 Desc64;
00871         if(Description == NULL)
00872             Desc256 = Path->GetTruncatedPath(60);
00873         else
00874             Desc256 = *Description;
00875         Desc256.Left(&Desc64, 64);
00876                                             
00877         // The filename of the index
00878         String_64 IndexFilename(Path->GetFileName(TRUE));
00879 
00880         // Add this new index to the library file and scan it in...
00881         if(NewSubLib->Init(ParentGal, &FilesPath, &Desc64, &IndexFilename, Type, FALSE, (Description != NULL)))
00882         {
00883             Libraries.AddTail(NewSubLib);
00884 
00885             // Keep track of libraries added for redraw purposes...
00886             AddNewFolderToScrollRedrawSystem(NewSubLib);
00887 
00888             return TRUE;
00889         }
00890         else
00891         {
00892             // if there was a problem, don't go round leaving memory everywhere...
00893             delete NewSubLib;
00894             NewSubLib = NULL;
00895         }
00896     }
00897 
00898     // Nothing added
00899     return FALSE;
00900 }
00901 
00902 
00903 
00904 /***********************************************************************************************
00905 
00906 >   static BOOL LibraryFile::TidyUpSubPath(String_256 *Str)
00907 
00908     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00909     Created:    12/4/95
00910 
00911     Inputs:     Str - The input string
00912     Outputs:    Str - The output string
00913     Returns:    TRUE if things went ok
00914 
00915     Purpose:    Removes trailing backslashes and trailing xarainfo's from the string.
00916                 This means we can browse to a xarainfo directory then jump back a place
00917                 to get the correct library subpath, etc...
00918 
00919                 There's also a 'special case' for root directories... Instead of returning
00920                 "C:" we return "C:\". This will not work with unc stuff, but then neither
00921                 will most of the gallery code :-(
00922 
00923 ***********************************************************************************************/
00924 
00925 BOOL LibraryFile::TidyUpSubPath(String_256 *Str)
00926 {
00927     if(Str == NULL)
00928     {   
00929         ERROR3("LibraryFile::TidyUpSubPath given a null string");
00930         return FALSE;
00931     }
00932 
00933     String_256 OurPath(*Str);
00934 
00935     // Remove a trailing '\' if there is one
00936     if(((TCHAR *)OurPath)[OurPath.Length() - 1] == '\\')
00937     {
00938         Str->Left(&OurPath, Str->Length() - 1);
00939     }
00940 
00941     if(OurPath.Length() > 10)
00942     {
00943         // Remove a 'XaraInfo' if there is one on the end
00944         String_256 OurPathTmp(OurPath); 
00945         String_16 RighthandSection;
00946         OurPathTmp.Right(&RighthandSection, 9);
00947         RighthandSection.toUpper();
00948 
00949         // \\XARAINFO
00950         String_16 XaraInfo("\\" + String_16(_R(IDS_LIBRARIES_XARAINFO_DIRNAME_CAPS)));
00951         if(camStrcmp((TCHAR *)RighthandSection, (TCHAR *)XaraInfo) == 0)
00952         {
00953             // Use the un-uppered string to return
00954             OurPathTmp.Left(&OurPath, OurPath.Length() - 9);
00955         }
00956     }
00957 
00958     // Check if root directory, if so, add in a trailing '\' (fixes root directory problems)
00959     if(SGLibOil::IsRootDirectory(&OurPath))
00960     {
00961         //ERROR3("Drive name given - appending a slash");
00962         OurPath += TEXT("\\");
00963     }
00964 
00965     *Str = OurPath;
00966     return TRUE;
00967 }
00968 
00969 /***********************************************************************************************
00970 
00971 >   static INT32 LibraryFile::CountLines(PathName *IndexFilePath)
00972 
00973     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
00974     Created:    13/4/95
00975 
00976     Inputs:     IndexFilePath - pathname of file we're interested in
00977     Outputs:
00978     Returns:    Number of lines found
00979 
00980     Purpose:    Count number of valid lines in a file
00981 
00982 ***********************************************************************************************/
00983 
00984 INT32 LibraryFile::CountLines(PathName *IndexFilePath)
00985 {
00986     INT32 Count = 0;
00987 
00988     CCDiskFile TmpDiskFile;
00989     if (!TmpDiskFile.InitLexer(FALSE))
00990     {
00991         ERROR3("Library::CountLines - Problems with InitLexer");
00992         return 0;
00993     }
00994 
00995     if (TmpDiskFile.open(*IndexFilePath, ios::in))
00996     {
00997         BOOL Finished = FALSE;;
00998         LexTokenType TokType = TOKEN_LINE;
00999         while(TmpDiskFile.GetLineToken() && TokType != TOKEN_EOF)
01000         {
01001             TokType = TmpDiskFile.GetTokenType();
01002                 if(TokType == TOKEN_LINE)
01003                     Count ++;
01004         }
01005     }
01006     
01007     // reclaim lexer-buffer memory
01008     TmpDiskFile.DeinitLexer();
01009 
01010     if(TmpDiskFile.isOpen())
01011         TmpDiskFile.close();
01012 
01013     return Count;
01014 }
01015     
01016 /***********************************************************************************************
01017 
01018 >   BOOL LibraryFile::KillLeadingSpaces(StringBase *Str)
01019 
01020     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
01021     Created:    21/2/95
01022 
01023     Inputs:     Str - The string to have leading spaces killed
01024     Outputs:    Str - modified to have leading spaces stripped off
01025 
01026     Returns:    FALSE in a major crisis situation, which, looking at the code is pretty unlikely
01027                 since it only ever returns TRUE.
01028 
01029     Purpose:    Strip any leading AND NOW TRAILING spaces from the given string.
01030 
01031 ***********************************************************************************************/
01032 
01033 BOOL LibraryFile::KillLeadingSpaces(StringBase *Str)
01034 {
01035     ERROR3IF(Str == NULL, "LibraryFile::KillLeadingSpaces - NULL params are illegal");
01036 
01037     if( ((TCHAR *)(*Str))[0] != ' ' && ((TCHAR *)(*Str))[Str->Length()-1] != ' ')
01038         return TRUE;
01039 
01040     INT32 Length = Str->Length();
01041     INT32 FirstNonSpaceChar = 0;
01042     while (((TCHAR *)(*Str))[FirstNonSpaceChar] == ' ' && FirstNonSpaceChar < Length)
01043         FirstNonSpaceChar++;
01044 
01045     if(FirstNonSpaceChar >= Length)
01046         *Str = "";
01047     else
01048     {
01049         // Strip off trailing spaces...
01050         INT32 LastNonSpaceChar = Length;
01051         while (((TCHAR *)(*Str))[LastNonSpaceChar-1] == ' ' && LastNonSpaceChar > 0)
01052             LastNonSpaceChar--;
01053 
01054         String_256 Str256;
01055         Str->Mid(&Str256, FirstNonSpaceChar, LastNonSpaceChar - FirstNonSpaceChar);
01056         *Str = Str256;
01057     }
01058 
01059     return TRUE;
01060 }
01061 
01062 
01063 /***********************************************************************************************
01064 
01065 >   PathName *LibraryFile::GetLibraryPathName(void)
01066 
01067     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
01068     Created:    24/2/95
01069 
01070     Returns:    The disc pathname of this library file
01071 
01072     Purpose:    To determine the pathname with which this library was initialised
01073 
01074     SeeAlso:    LibraryFile::Init
01075 
01076 ***********************************************************************************************/
01077 
01078 PathName *LibraryFile::GetLibraryPathName(void)
01079 {
01080     // Did the client forget to call Init() before calling this method?
01081     ERROR3IF(MyType == SGLib_Blank, "This LibraryFile has not been properly initialised!");
01082 
01083     return(&MyPath);
01084 }
01085 
01086 
01087 
01088 /***********************************************************************************************
01089 
01090 >   SGLibType LibraryFile::GetLibraryType(void)
01091 
01092     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
01093     Created:    24/2/95
01094 
01095     Returns:    The library type of this library file
01096 
01097     Purpose:    To determine the librry type of this library file and its Libraries
01098 
01099     SeeAlso:    LibraryFile::Init
01100 
01101 ***********************************************************************************************/
01102 
01103 SGLibType LibraryFile::GetLibraryType(void)
01104 {
01105     // Did the client forget to call Init() before calling this method?
01106     ERROR3IF(MyType == SGLib_Blank, "This LibraryFile has not been properly initialised!");
01107 
01108     return(MyType);
01109 }
01110 
01111 
01112 /***********************************************************************************************
01113 
01114 >   static BOOL LibraryFile::GenerateDefaultIndex(PathName *IndexFile, PathName *LibDirPath,
01115                                                         SGLibType Type, BOOL Thumbnails)
01116 
01117     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
01118     Created:    2/4/95
01119 
01120     Inputs:     IndexFile - Full path of index file to generate - C:\testlib\animals\xarainfo\xaraclip.txt
01121                 LibDirPath - Full path of sublibrary - C:\testlib\animals
01122                 Author - Default author for index entries
01123                 Type - Type of library index required 
01124                 Thumbnails - Generate thumbnails if we can ?
01125     Outputs:
01126     Returns:    TRUE if things went ok.
01127 
01128     Purpose:    Generates a dummy index file for a directory of say .art files.
01129 
01130 ***********************************************************************************************/
01131 
01132 BOOL LibraryFile::GenerateDefaultIndex(PathName *IndexFile, PathName *LibDirPath, String_64 *Author,
01133                                                         SGLibType Type, BOOL Thumbnails)
01134 {
01135     if(IndexFile == NULL || LibDirPath == NULL)
01136     {
01137         ERROR3("LibraryFile::GenerateDefaultIndex null params are bad");
01138         return FALSE;
01139     }
01140 
01141     GenerateIndexFile TmpGen;
01142     BOOL ok = TmpGen.Generate(IndexFile, LibDirPath, Author, Type, Thumbnails);
01143 
01144     return ok;
01145 }
01146 
01147 
01148 
01149 
01150 
01151 
01152 
01153 /***********************************************************************************************
01154 
01155 >   void LibraryFile::InitScrollRedrawSystem(void)
01156 
01157     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
01158     Created:    1/9/95
01159 
01160     Inputs:
01161     Outputs:
01162     Returns:
01163     Purpose:    Resets the static vars...
01164     Notes:
01165     SeeAlso:
01166 
01167 ***********************************************************************************************/
01168 
01169 void LibraryFile::InitScrollRedrawSystem(void)
01170 {
01171     FirstLibraryAdded = NULL;
01172     LastLibraryAdded = NULL;
01173 }
01174 
01175 /***********************************************************************************************
01176 
01177 >   void LibraryFile::AddNewFolderToScrollRedrawSystem(Library *NewFolder)
01178 
01179     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
01180     Created:    1/9/95
01181 
01182     Inputs:
01183     Outputs:
01184     Returns:
01185     Purpose:    Adds a new library to the Scroll / redraw system
01186     Notes:
01187     SeeAlso:
01188 
01189 ***********************************************************************************************/
01190 
01191 void LibraryFile::AddNewFolderToScrollRedrawSystem(Library *NewFolder)
01192 {
01193     // Keep track of libraries added for redraw purposes...
01194     if(FirstLibraryAdded == NULL)
01195         FirstLibraryAdded = NewFolder;
01196     LastLibraryAdded = NewFolder;
01197 }
01198 
01199 /***********************************************************************************************
01200 
01201 >   void LibraryFile::DoScrollRedraw(void)
01202 
01203     Author:     Richard_Millican (Xara Group Ltd) <camelotdev@xara.com>
01204     Created:    1/9/95
01205     Scope:      Public
01206     Purpose:    Scrolls to newly added groups...
01207 
01208 ***********************************************************************************************/
01209 
01210 void LibraryFile::DoScrollRedraw(void)
01211 {
01212     // Scroll / redraw the newly added groups...
01213     if(FirstLibraryAdded != NULL && LastLibraryAdded != NULL)
01214     {
01215         SGLibGroup *FirstGroup = FirstLibraryAdded->ParentGroup;
01216         SGLibGroup *SecondGroup = LastLibraryAdded->ParentGroup;
01217     
01218         if(FirstGroup != NULL && SecondGroup != NULL)
01219         {
01220             // Redraw whatever we need to and reformat the gallery ready for poking...
01221             ParentGallery->ReformatAndRedrawIfNecessary();
01222 
01223             // Calculate where the new folders are...
01224             DocRect ScrollToRect;
01225             FirstGroup->GetFormatRect(&ScrollToRect);
01226             if(SecondGroup != FirstGroup)
01227             {
01228                 DocRect TempRect;
01229                 FirstGroup->GetFormatRect(&TempRect);               
01230                 ScrollToRect = ScrollToRect.Union(TempRect);
01231             }
01232 
01233             // Actually do the scroll...
01234             ParentGallery->ScrollToShow(&ScrollToRect, TRUE);
01235         }
01236     }
01237 }

Generated on Sat Nov 10 03:46:58 2007 for Camelot by  doxygen 1.4.4