00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <gtk/gtk.h>
00036 #include <gdk-pixbuf/gdk-pixbuf.h>
00037 #include <strings.h>
00038
00039 #include <libermanifest/ermanifest.h>
00040
00041 #include "FileManager.h"
00042 #include "FileMgr_page.h"
00043 #include "FileMgr_xml.h"
00044 #include "ScribbleDefine.h"
00045 #include "ScribbleLog.h"
00046 #include "ScribbleUtils.h"
00047
00048
00049 static int get_result_dirname(char *sResultDir, int iSize,const char* sDataDir);
00050
00051
00052
00053
00054 int createDirHirachy(PFileManager pFM)
00055 {
00056
00057 const char* sDataDir=pFM->sDataDir;
00058 if( pFM->bNeedCreatDirHirachy && !FileExist(sDataDir) )
00059 {
00060 SB_FMPRINTF("create for\n%s\n",sDataDir);
00061 if( 0!=CreateDir(sDataDir))
00062 {
00063 SB_ERRORPRINTF("create directory [%s]fail\n",sDataDir);
00064 return -1;
00065 }
00066 SB_FMPRINTF("create directory [%s] ok\n",sDataDir);
00067 char sResultDir[MAX_PATHNAME+1];
00068
00069 get_result_dirname(sResultDir,sizeof(sResultDir),sDataDir);
00070 if( 0!=CreateDir(sResultDir))
00071 {
00072 SB_ERRORPRINTF("create directory [%s]fail\n",sResultDir);
00073 return -1;
00074 }
00075 SB_FMPRINTF("create directory [%s] ok\n",sResultDir);
00076
00077
00078 if( 0!=fm_initIrxHandle(pFM,FALSE) )
00079 {
00080 SB_ERRORPRINTF("create .irx inkfile error\n");
00081 return -1;
00082 }
00083 pFM->bNeedCreatDirHirachy=FALSE;
00084 SB_FMPRINTF("done\n");
00085 }
00086
00087 return 0;
00088 }
00089
00090
00091 void dumpFileManager(PFileManager pFM)
00092 {
00093
00094 SB_FMPRINTF("\n==========================================\n"
00095 "sTmplPathName=[%s]\n"
00096 "sManifestPathName=[%s]\n"
00097 "sDataDir=[%s]\n"
00098 "sScribbleName=[%s]\n"
00099 "========================================\n",
00100 pFM->sTmplPathName,
00101 pFM->sManifestPathName,
00102 pFM->sDataDir,
00103 pFM->manifest.sScribbleName);
00104 }
00105
00106
00107 int initFileManagerByTmpl(PFileManager pFM,const char* sTmplPathName)
00108 {
00109 SB_FMPRINTF("init by %s\n",sTmplPathName);
00110 int iLenOfSuffix=strlen(".bmp");
00111 if( NULL==sTmplPathName || strlen(sTmplPathName)<=iLenOfSuffix )
00112 return -1;
00113
00114 memset(pFM,0,sizeof(FileManager));
00115
00116 strncpy(pFM->sTmplPathName,sTmplPathName,
00117 sizeof(pFM->sTmplPathName)-1);
00118
00119 GetDirFromFullName(pFM->sDataDir,sizeof(pFM->sDataDir),sTmplPathName);
00120
00121 if( 0!=getUniqNameForSCRIB(pFM) )
00122 {
00123 return -1;
00124 }
00125
00126 strcat(pFM->sDataDir,pFM->manifest.sScribbleName);
00127 strcat(pFM->sDataDir,"/");
00128
00129
00130 strcpy(pFM->sManifestPathName,pFM->sDataDir);
00131 strcat(pFM->sManifestPathName,MANIFEST_FILENAME);
00132
00133 dumpFileManager(pFM);
00134
00135 pFM->manifest.iLastOpenPage=0;
00136 pFM->manifest.iTotalPages=0;
00137
00138 pFM->bNeedCreatDirHirachy=TRUE;
00139 pFM->isCreatedFromTmpl=TRUE;
00140
00141
00142 insertNewPage(pFM,1,NULL);
00143 jumptoScribPage(pFM,1);
00144
00145 return 0;
00146 }
00147
00148 int initFileManagerByManifest(PFileManager pFM,const char* sManifestPathName)
00149 {
00150 SB_FMPRINTF("init by \n%s\n",sManifestPathName);
00151 memset(pFM,0,sizeof(FileManager));
00152 strncpy(pFM->sManifestPathName,sManifestPathName,
00153 sizeof(pFM->sManifestPathName)-1 );
00154 if( 0!=loadManifest(pFM) )
00155 {
00156 SB_ERRORPRINTF("load manifest[%s] error\n",sManifestPathName);
00157 return -1;
00158 }
00159
00160 GetDirFromFullName(pFM->sDataDir,sizeof(pFM->sDataDir),sManifestPathName);
00161
00162 pFM->isCreatedFromTmpl=FALSE;
00163
00164 SB_FMPRINTF("notes type is:[%s]",pFM->manifest.sInkType);
00165
00166 if( 0!=strcasecmp(pFM->manifest.sInkType,SCRIB_TYPE) )
00167 {
00168 SB_FMPRINTF("init by \n%s\n",sManifestPathName);
00169 if( 0!=fc_convertold2irx(pFM) )
00170 {
00171 SB_ERRORPRINTF("converting error\n");
00172 return -1;
00173 }
00174 pFM->bNeedSaveManifest=TRUE;
00175 SB_FMPRINTF("update manifest...");
00176 strncpy(pFM->manifest.sInkType,SCRIB_TYPE,sizeof(pFM->manifest.sInkType));
00177
00178 if( 0!=fm_setIrxTypeAndSave(pFM) )
00179 {
00180 SB_ERRORPRINTF("save type error[%s]\n",pFM->sManifestPathName);
00181 return -1;
00182 }
00183 }
00184
00185 if( 0!=fm_initIrxHandle(pFM,TRUE) )
00186 {
00187 SB_ERRORPRINTF("open .irx inkfile error\n");
00188 return -1;
00189 }
00190
00191 pFM->bNeedSaveManifest=FALSE;
00192
00193 PScribPage pScribPage=jumptoScribPage(pFM,pFM->manifest.iLastOpenPage);
00194 if( NULL==pScribPage )
00195 return -1;
00196
00197 strcpy(pFM->sTmplPathName,pScribPage->sImgFileName);
00198
00199 pFM->bNeedCreatDirHirachy=FALSE;
00200
00201 dumpFileManager(pFM);
00202 return 0;
00203 }
00204
00205 void FreeFileManager(PFileManager pFM)
00206 {
00207 if( NULL==pFM ) return;
00208
00209 PScribPage pCurrPage=pFM->manifest.pFirstScribPage;
00210 PScribPage pNextPage=NULL;
00211 while(pCurrPage)
00212 {
00213 pNextPage=pCurrPage->pNextPage;
00214
00215 freeScribPage(pCurrPage);
00216 pCurrPage=pNextPage;
00217 }
00218 if( NULL!=pFM->irxHandle)
00219 {
00220 free(pFM->irxHandle);
00221 pFM->irxHandle=NULL;
00222 }
00223 memset(pFM,0,sizeof(FileManager));
00224 }
00225
00226
00227
00228 char* getAbsPathFromCurrScrib(char* sAbsFileName,
00229 int iSize,
00230 const char* currFileName,
00231 const PFileManager pFM)
00232 {
00233 memset(sAbsFileName,0,iSize);
00234 if( currFileName[0] == '/' )
00235 {
00236 strncpy(sAbsFileName,currFileName,iSize-1);
00237 }
00238 else
00239 {
00240
00241 snprintf( sAbsFileName,iSize-1,
00242 "%s%s",
00243 pFM->sDataDir,
00244 currFileName);
00245 }
00246 return sAbsFileName;
00247 }
00248
00249
00250 int get_ink_filename (char *inkFileName, int iSize,
00251 const PFileManager pFM)
00252 {
00253
00254 memset(inkFileName,0,iSize);
00255 snprintf( inkFileName,iSize-1,
00256 "%s%s.irx",
00257 pFM->sDataDir,
00258 pFM->manifest.sScribbleName);
00259
00260 return 0;
00261 }
00262
00263
00264
00265 static int get_result_dirname(char *sResultDir, int iSize,const char* sDataDir)
00266 {
00267 SB_FMPRINTF("__start__\n");
00268 memset(sResultDir,0,iSize);
00269 snprintf(sResultDir,iSize-1, "%sresult",sDataDir);
00270 SB_FMPRINTF("__end__\nresultdir=%s\n", sResultDir);
00271 return 0;
00272 }
00273
00274
00275 int get_result_filename (char *resultFileName, int iSize,
00276 const PFileManager pFM,const PScribPage pScribPage)
00277 {
00278
00279 if( NULL==pScribPage) return -1;
00280
00281 memset(resultFileName,0,iSize);
00282 snprintf( resultFileName,iSize-1,
00283 "%sresult/%s.png",
00284 pFM->sDataDir,
00285 pScribPage->sPageName);
00286
00287 return 0;
00288 }
00289
00290
00291 int get_curr_result_filename (char *resultFileName, int iSize,const PFileManager pFM)
00292 {
00293 return get_result_filename(resultFileName,iSize,
00294 pFM,getCurrScribPage(pFM));
00295 }
00296
00297
00298 void dumpManifest(Manifest *pM)
00299 {
00300 printf("\n=================================\n"
00301 "sScribbleName=[%s]\n"
00302 "iLastOpenPage=[%d]\n"
00303 "iTotalPages=[%d]\n",
00304 pM->sScribbleName,pM->iLastOpenPage,pM->iTotalPages);
00305 PScribPage pPage=pM->pFirstScribPage;;
00306 while(pPage)
00307 {
00308 printf("----------------------------------\n");
00309 printf("sImgFileName=[%s]\nsPageName=[%s]\n",
00310 pPage->sImgFileName,pPage->sPageName);
00311 printf("sImgFileName=[%s]\nsPageName=[%s]\nbDirty=%d\n",
00312 pPage->sImgFileName,pPage->sPageName,pPage->bDirty);
00313
00314 pPage=pPage->pNextPage;
00315 }
00316 printf("=================================\n");
00317 }