tmplmngr.cpp

Go to the documentation of this file.
00001 // $Id: app.cpp 1437 2006-07-13 09:05:53Z luke $
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 
00099 #include "camtypes.h"
00100 
00101 #include "camelot.h"
00102 #include "sgliboil.h"   // AppendSlashIfNotPresent
00103 #include "fileutil.h"
00104 
00105 #include "tmplmngr.h"
00106 
00107 DECLARE_SOURCE("$Revision: 1437 $");
00108 
00109 /********************************************************************************************
00110 
00111     Preference:     Path
00112     Section:        Templates
00113     Range:          0 .. 256 characters
00114     Purpose:        The path to load the templates from and save the templates to.
00115                     Defaults to blank which means alongside the exe.
00116     SeeAlso:        CCamDoc::LoadDefaultDocument; OpMenuSave::Do;
00117 
00118 ********************************************************************************************/
00119 
00120 String_256 CTemplateManager::m_TemplatesPath = TEXT("");
00121 String_256 CTemplateManager::m_LocalTemplatesPath = TEXT("");
00122 
00123 /********************************************************************************************
00124 
00125     Preference:     DefaultAnimationTemplate
00126     Section:        Templates
00127     Range:          String
00128     Purpose:        The path to the default animation template. By default, this
00129                     is the EXE path with "\templates\animation.xar" added to the end
00130     SeeAlso:        -
00131 
00132 ********************************************************************************************/
00133 
00134 String_256 CTemplateManager::ms_strDefaultAnimationTemplate = _T("");
00135 
00136 /********************************************************************************************
00137 
00138     Preference:     DefaultDrawingTemplate
00139     Section:        Templates
00140     Range:          String
00141     Purpose:        The path to the default drawing template. By default, this
00142                     is the EXE path with "\templates\drawing.xar" added to the end
00143     SeeAlso:        -
00144 
00145 ********************************************************************************************/
00146 
00147 String_256 CTemplateManager::ms_strDefaultDrawingTemplate = _T("");
00148 
00149 
00150 /***********************************************************************************************
00151 
00152 >   CTemplateManager::CTemplateManager()
00153 
00154     Author:     Luke_Hart (Xara Group Ltd) <lukeh@xara.com>
00155     Created:    17/07/2006
00156     
00157     Inputs:     -
00158     Outputs:    -
00159     Returns:    -
00160                  
00161     Purpose:    Initialise the manager.
00162                
00163     Errors:     None.
00164                                                                  
00165 ***********************************************************************************************/
00166 
00167 CTemplateManager::CTemplateManager()
00168 {
00169 }
00170 
00171 /***********************************************************************************************
00172 
00173 >   CTemplateManager::~CTemplateManager()
00174 
00175     Author:     Luke_Hart (Xara Group Ltd) <lukeh@xara.com>
00176     Created:    17/07/2006
00177     
00178     Inputs:     - 
00179     Outputs:    -
00180     Returns:    -
00181                  
00182     Purpose:    Destroys the manager.
00183                
00184     Errors:     None.
00185                                                                  
00186 ***********************************************************************************************/
00187 
00188 CTemplateManager::~CTemplateManager()
00189 {
00190 }
00191 
00192 
00193 /***********************************************************************************************
00194 
00195 >   BOOL CTemplateManager::Init()
00196 
00197     Author:     Luke_Hart (Xara Group Ltd) <lukeh@xara.com>
00198     Created:    17/07/2006
00199     
00200     Inputs:     -
00201     Outputs:    -
00202     Returns:    TRUE if the object initialised ok.
00203                  
00204     Purpose:    Initialise the manger
00205 
00206     Errors:     None.
00207                                                                  
00208 ***********************************************************************************************/
00209 
00210 BOOL CTemplateManager::Init()
00211 {
00212     //Graham 20/10/97
00213     if (Camelot.DeclareSection( _T("NewTemplates"), 10))
00214     {
00215         Camelot.DeclarePref( _T("NewTemplates"), _T("DefaultAnimationFile"), &ms_strDefaultAnimationTemplate);
00216         Camelot.DeclarePref( _T("NewTemplates"), _T("DefaultDrawingFile"), &ms_strDefaultDrawingTemplate);
00217     }
00218 
00219     if (ms_strDefaultAnimationTemplate==String_256(_T("")))
00220     {
00221         String_256 strNameOfAnimationTemplate( _R(IDS_NEWTEMPLATES_DEFAULTANIMATIONFILE) );
00222         String_256 strPathOfTemplate( GetTemplatesPath() );
00223         strPathOfTemplate += strNameOfAnimationTemplate;
00224 
00225         ms_strDefaultAnimationTemplate=strPathOfTemplate;
00226 
00227         TRACEUSER( "jlh92", _T("DefAnimTempl = %s\n"), PCTSTR(ms_strDefaultAnimationTemplate) );
00228     }
00229 
00230     if( ms_strDefaultDrawingTemplate == String_256( _T("") ) || 
00231         0 == camStrcmp( ms_strDefaultDrawingTemplate, _T("default.xar") ) )
00232     {
00233         ms_strDefaultDrawingTemplate=String_256(_R(IDS_DEFAULTDOCNAME));
00234 
00235 PORTNOTETRACE("other","CTemplateManager::Init - remove code to setup paths");
00236 #if !defined(EXCLUDE_FROM_XARALX)
00237         //Then assume it's the exe path with \templates\drawing.xar on the end
00238         TCHAR Pathname[MAX_PATH];
00239 
00240         if(GetModuleFileName(NULL, Pathname, MAX_PATH) == 0) //Should be in the winoil really
00241             return FALSE;
00242 
00243         // Put the path name into a string
00244         String_256 strPathOfExe(Pathname);
00245         PathName pathPathOfExe(strPathOfExe);
00246 
00247         strPathOfExe = pathPathOfExe.GetLocation(TRUE);
00248 
00249         //And add "templates\" to the end
00250         String_256 strTemplatesDirectory(_R(IDS_NEWTEMPLATES_RELATIVEPATH));
00251         String_256 strNameOfDrawingTemplate(_R(IDS_DEFAULTDOCNAME));
00252 
00253         String_256 strPathOfTemplate=strPathOfExe;
00254         strPathOfTemplate+=strTemplatesDirectory;
00255         strPathOfTemplate+=strNameOfDrawingTemplate;
00256 
00257         ms_strDefaultDrawingTemplate=strPathOfTemplate;
00258 #endif
00259     }
00260 
00261     // As the preference system is up, declare our preference to it
00262     // This is the pathname that the templates should be loaded from and saved to
00263     // If blank, the default, then it should use the exe path
00264     GetApplication()->DeclareSection(TEXT("Templates"), 2);
00265     GetApplication()->DeclarePref(TEXT("Templates"), TEXT("Path"), &m_TemplatesPath);
00266     GetApplication()->DeclarePref(TEXT("Templates"), TEXT("LocalPath"), &m_LocalTemplatesPath);
00267     
00268     //Graham 21/10/97: If it is blank, then we should use the
00269     //exe path with "\templates\" on the end
00270     if( m_TemplatesPath.IsEmpty() || !SGLibOil::DirExists( m_TemplatesPath ) )
00271     {
00272         // Put the path name into a string
00273         m_TemplatesPath = CCamApp::GetResourceDirectory();
00274 
00275         //And add "templates\" to the end
00276         String_256 strRelativePath(_R(IDS_NEWTEMPLATES_RELATIVEPATH));
00277 
00278         m_TemplatesPath+=strRelativePath;
00279 
00280 #if defined(_DEBUG)
00281         if( !SGLibOil::DirExists( m_TemplatesPath ) )
00282             m_TemplatesPath = _T("/usr/share/xaralx/Templates/");
00283 #endif
00284     }
00285 
00286     if( m_LocalTemplatesPath.IsEmpty() || !SGLibOil::DirExists( m_LocalTemplatesPath ) )
00287     {
00288         wxString        strHome( ::wxGetHomeDir() );
00289         m_LocalTemplatesPath = (PCTSTR)strHome;
00290         m_LocalTemplatesPath += _T("/.xaralx/templates/");
00291     }
00292 
00293     TRACEUSER( "jlh92", _T("Using %s as template store\n"), PCTSTR(m_TemplatesPath) );
00294     return TRUE;
00295 }
00296 
00297 
00298 /***********************************************************************************************
00299 
00300 >   String_256& CTemplateManager::GetTemplatesPath()
00301 
00302     Author:     Neville_Humphrys (Xara Group Ltd) <camelotdev@xara.com>
00303     Created:    03/03/97
00304     Inputs:     -
00305     Outputs:    -
00306     Returns:    The specfied templates path in a string
00307     Purpose:    To find out the user's preference for a path to the template file.
00308     SeeAlso:    CCamDoc::LoadDefaultDocument; OpMenuSave::Do;
00309 
00310 ***********************************************************************************************/
00311 
00312 String_256& CTemplateManager::GetTemplatesPath()
00313 {
00314     // Add a trailing slash if it hasn't got one
00315     SGLibOil::AppendSlashIfNotPresent(&m_LocalTemplatesPath);
00316 
00317     return m_LocalTemplatesPath;
00318 }
00319 
00320 /***********************************************************************************************
00321 
00322 >   void CTemplateManager::SetTemplatesPath(String_256& strToSet)
00323 
00324     Author:     Graham_Walmsley (Xara Group Ltd) <camelotdev@xara.com>
00325     Created:    28/20/97
00326     Inputs:     strToSet - the new value
00327     Returns:    -
00328     Purpose:    To set the default templates path
00329     SeeAlso:    -
00330 
00331 ***********************************************************************************************/
00332 
00333 void CTemplateManager::SetTemplatesPath(String_256& strToSet)
00334 {
00335     m_LocalTemplatesPath = strToSet;
00336 
00337     // Add a trailing slash if it hasn't got one
00338     SGLibOil::AppendSlashIfNotPresent(&m_LocalTemplatesPath);
00339 }
00340 
00341 /********************************************************************************************
00342 
00343 >   PathName CTemplateManager::GetDefaultAnimationTemplate()
00344 
00345     Author:         Graham_Walmsley (Xara Group Ltd) <camelotdev@xara.com>
00346     Created:        23/10/97
00347     Inputs:         -
00348     Outputs:        -
00349     Returns:        The path of the default animation template
00350     Purpose:        As above
00351     Errors:         -
00352     SeeAlso:        -
00353 
00354 ********************************************************************************************/
00355 
00356 PathName CTemplateManager::GetDefaultAnimationTemplate()
00357 {
00358     PathName pathToReturn=ms_strDefaultAnimationTemplate;
00359 
00360     return pathToReturn;
00361 }
00362 
00363 /********************************************************************************************
00364 
00365 >   PathName CTemplateManager::GetDefaultDrawingTemplate()
00366 
00367     Author:         Graham_Walmsley (Xara Group Ltd) <camelotdev@xara.com>
00368     Created:        23/10/97
00369     Inputs:         -
00370     Outputs:        -
00371     Returns:        The path of the default drawing template
00372     Purpose:        As above
00373     Errors:         -
00374     SeeAlso:        -
00375 
00376 ********************************************************************************************/
00377 
00378 PathName CTemplateManager::GetDefaultDrawingTemplate()
00379 {
00380     PathName pathToReturn=ms_strDefaultDrawingTemplate;
00381 
00382     return pathToReturn;
00383 }
00384 
00385 
00386 /********************************************************************************************
00387 
00388 >   PathName CTemplateManager::GetTemplateList( CTemplateList* pList, const String_256& strTemplatePath, bool fLocal )
00389 
00390     Author:         Luke_Hart (Xara Group Ltd) <lukehart@xara.com>
00391     Created:        18/07/2006
00392     Inputs:         const String_256& strTemplatePath   Path to directory to scan
00393                     bool fLocal                         Value local flag to stroe in list 
00394     Outputs:        CTemplateList* pList                The returned list
00395     Returns:        Nothing
00396     Purpose:        INTERNAL - Fill the list of templates with all templates in specified directory,
00397                     marking each with specified user local flag
00398     Errors:         -
00399     SeeAlso:        -
00400 
00401 ********************************************************************************************/
00402 
00403 void CTemplateManager::GetTemplateList( CTemplateList* pList, const String_256& strTemplatePath, bool fLocal )
00404 {
00405     // Don't bother with any of this is directory is invalid
00406     if( !wxDir::Exists( (PCTSTR)strTemplatePath ) )
00407         return;
00408 
00409     //Now search that path for templates
00410     //Start by setting the leaf name to *.xar
00411     String_256          strSearchFilename( _R(IDS_NEWTEMPLATES_DEFAULTTEMPLATEEXTENSION) );
00412 
00413     // Get the default entry names
00414     String_256          strPathOfDrawingTemplate   = CTemplateManager::GetDefaultDrawingTemplate().GetPath(FALSE);
00415     strPathOfDrawingTemplate.SwapChar( _T('_'), _T(' ') );
00416     String_256          strPathOfAnimationTemplate = CTemplateManager::GetDefaultAnimationTemplate().GetPath(FALSE);
00417     strPathOfAnimationTemplate.SwapChar( _T('_'), _T(' ') );
00418     String_256          strPathOfFile;
00419 
00420     TRACEUSER( "jlh92", _T("DefPath = %s, %s\n"), PCTSTR(strPathOfDrawingTemplate),
00421         PCTSTR(strPathOfAnimationTemplate) );
00422 
00423     // Build system template path
00424     PathName            pathTemplates( strTemplatePath );
00425     pathTemplates.SetFileNameAndType( strSearchFilename );
00426     PathName            pathOfFile( pathTemplates );
00427     String_256          strTemplates = pathTemplates.GetPath( FALSE );
00428 
00429     //And search the path for xar files that are
00430     //NOT the default animation or drawing templates
00431     String_256          strNameOfFile;
00432     if( FileUtil::StartFindingFiles( &strTemplates ) )
00433     {
00434         while( FileUtil::FindNextFile( &strNameOfFile ) )
00435         {
00436             pathOfFile.SetFileNameAndType( strNameOfFile );
00437             strPathOfFile = pathOfFile.GetFileName(TRUE);
00438 
00439             if( 0 != strPathOfFile.CompareTo( strPathOfDrawingTemplate, FALSE ) &&
00440                 0 != strPathOfFile.CompareTo( strPathOfAnimationTemplate, FALSE ) )
00441             {
00442                 (*pList)[strPathOfFile] = fLocal;
00443                 TRACEUSER( "jlh92", _T("Curr = %s\n"), PCTSTR(strPathOfFile) );
00444             }
00445         }
00446         FileUtil::StopFindingFiles();
00447     }
00448 
00449     // Don't allow any errors set while searching to propagate outside this scope
00450     Error::ClearError();
00451 }
00452 
00453 
00454 /********************************************************************************************
00455 
00456 >   PathName CTemplateManager::GetTemplateMenuName( UINT32 ordNumberOfTemplate, String_256* pStrNameOfFile )
00457 
00458     Author:         Luke_Hart (Xara Group Ltd) <lukehart@xara.com>
00459     Created:        18/07/2006
00460     Inputs:         UINT32 ordNumberOfTemplate      Ordinal of template to query
00461     Outputs:        String_256* pStrNameOfFile      Name that is returned
00462     Returns:        True on success, else false if template with ordinal doesn't exist
00463     Purpose:        Return the name to use in the New menu in pStrNameOfFile for the template
00464                     with ordinal ordNumberOfTemplate.
00465     Errors:         -
00466     SeeAlso:        -
00467 
00468 ********************************************************************************************/
00469 
00470 bool CTemplateManager::GetTemplateMenuName( UINT32 ordNumberOfTemplate, String_256* pStrNameOfFile )
00471 {
00472     // Fill the list with all templates (user local, begin second will over-write system ones)
00473     CTemplateList       setSortFilename;
00474     GetTemplateList( &setSortFilename, m_TemplatesPath, false );
00475     GetTemplateList( &setSortFilename, m_LocalTemplatesPath, true );
00476 
00477     // If the template with ordinal doesn't exist, fail
00478     if( ordNumberOfTemplate > UINT32(setSortFilename.size()) )
00479         return false;
00480 
00481     // Get enbry for template
00482     CTemplateList::iterator iter = setSortFilename.begin();
00483     for( UINT32 i = 1; i < ordNumberOfTemplate; ++i, ++iter )
00484     { /*Do nothing!*/ }
00485 
00486     *pStrNameOfFile = iter->first;
00487     TRACEUSER( "jlh92", _T("Final(%d) = %s\n"), ordNumberOfTemplate, PCTSTR(*pStrNameOfFile) );
00488 
00489     return true;
00490 }
00491 
00492 
00493 /********************************************************************************************
00494 
00495 >   PathName CTemplateManager::GetTemplateFilename( UINT32 ordNumberOfTemplate, String_256* pStrNameOfFile )
00496 
00497     Author:         Luke_Hart (Xara Group Ltd) <lukehart@xara.com>
00498     Created:        18/07/2006
00499     Inputs:         UINT32 ordNumberOfTemplate      Ordinal of template to query
00500     Outputs:        String_256* pStrNameOfFile      Path that is returned
00501     Returns:        True on success, else false if template with ordinal doesn't exist
00502     Purpose:        Return the path to template with ordinal ordNumberOfTemplate in pStrNameOfFile.
00503     Errors:         -
00504     SeeAlso:        -
00505 
00506 ********************************************************************************************/
00507 
00508 bool CTemplateManager::GetTemplateFilename( UINT32 ordNumberOfTemplate, String_256* pStrNameOfFile )
00509 {
00510     // Fill the list with all templates (user local, begin second will over-write system ones)
00511     CTemplateList       setSortFilename;
00512     GetTemplateList( &setSortFilename, m_TemplatesPath, false );
00513     GetTemplateList( &setSortFilename, m_LocalTemplatesPath, true );
00514 
00515     // If the template with ordinal doesn't exist, fail
00516     if( ordNumberOfTemplate > UINT32(setSortFilename.size()) )
00517         return false;
00518 
00519     // Get enbry for template
00520     CTemplateList::iterator iter = setSortFilename.begin();
00521     for( UINT32 i = 1; i < ordNumberOfTemplate; ++i, ++iter )
00522     { /*Do nothing!*/ }
00523 
00524     // Pre-pend the path to the template
00525     PathName            pathTemplates = iter->second ? m_LocalTemplatesPath : m_TemplatesPath;
00526     pathTemplates.SetFileNameAndType( iter->first );
00527 
00528     *pStrNameOfFile = pathTemplates.GetPath();
00529     TRACEUSER( "jlh92", _T("Final(%d) = %s\n"), ordNumberOfTemplate, PCTSTR(*pStrNameOfFile) );
00530 
00531     return true;
00532 }

Generated on Sat Nov 10 03:48:57 2007 for Camelot by  doxygen 1.4.4