00001
00002
00003
00004
00005
00006 #include "stdafx.h"
00007 #include "FdmApp.h"
00008 #include "vmsDownloadsGroupsMgr.h"
00009 #include "mfchelp.h"
00010 #include "misc.h"
00011 #include "fsDownloads_GroupsMgr.h"
00012
00013 #ifdef _DEBUG
00014 #undef THIS_FILE
00015 static char THIS_FILE[]=__FILE__;
00016 #define new DEBUG_NEW
00017 #endif
00018
00019 vmsDownloadsGroupsMgr::vmsDownloadsGroupsMgr()
00020 {
00021 m_nGrpNextId = 1;
00022 }
00023
00024 vmsDownloadsGroupsMgr::~vmsDownloadsGroupsMgr()
00025 {
00026
00027 }
00028
00029 void vmsDownloadsGroupsMgr::CreateDefaultGroups()
00030 {
00031 vmsDownloadsGroupSmartPtr grp;
00032
00033 grp.CreateInstance ();
00034 grp->strName = LS (L_OTHER);
00035 if (IS_PORTABLE_MODE)
00036 grp->strOutFolder = "%sdrive%\\Downloads\\";
00037 else
00038 grp->strOutFolder = "C:\\Downloads\\";
00039 CString strRoot = grp->strOutFolder;
00040 grp->strExts = "";
00041 grp->nId = GRP_OTHER_ID;
00042 Add (grp, NULL, TRUE);
00043
00044 grp.CreateInstance ();
00045 grp->strName = "Video";
00046 grp->strOutFolder = strRoot + "Video\\";
00047 grp->strExts = GetVideoExts ();
00048 grp->nId = m_nGrpNextId++;
00049 Add (grp, NULL, TRUE);
00050
00051 grp.CreateInstance ();
00052 grp->strName = "Music";
00053 grp->strOutFolder = strRoot + "Music\\";
00054 grp->strExts = GetAudioExts ();
00055 grp->nId = m_nGrpNextId++;
00056 Add (grp, NULL, TRUE);
00057
00058 grp.CreateInstance ();
00059 grp->strName = "Software";
00060 grp->strOutFolder = strRoot + "Software\\";
00061 grp->strExts = "exe com msi";
00062 grp->nId = m_nGrpNextId++;
00063 Add (grp, NULL, TRUE);
00064 }
00065
00066 BOOL vmsDownloadsGroupsMgr::LoadFromDisk()
00067 {
00068 fsString strFile = fsGetDataFilePath ("groups.sav");
00069
00070 if (GetFileAttributes (strFile) == DWORD (-1))
00071 {
00072 fsDownloads_GroupsMgr grps;
00073 if (FALSE == grps.LoadGroups () || grps.GetCount () == 0)
00074 {
00075 CreateDefaultGroups ();
00076 return TRUE;
00077 }
00078
00079 for (int i = 0; i < grps.GetCount (); i++)
00080 {
00081 fsDownloadGroup grp;
00082 grps.GetGroup (&grp, i);
00083
00084 vmsDownloadsGroupSmartPtr grpNew;
00085 grpNew.CreateInstance ();
00086 grpNew->strName = grp.szName;
00087 grpNew->strOutFolder = grp.szOutFolder;
00088 grpNew->strExts = grp.szExts;
00089 grpNew->nId = grp.bOther ? GRP_OTHER_ID : m_nGrpNextId++;
00090 Add (grpNew, NULL, TRUE);
00091 }
00092
00093 return TRUE;
00094 }
00095
00096 HANDLE hFile = CreateFile (strFile, GENERIC_READ, FILE_SHARE_READ, NULL,
00097 OPEN_EXISTING, 0, NULL);
00098
00099 if (hFile != INVALID_HANDLE_VALUE)
00100 {
00101 vmsDownloadsGroupsFileHdr hdr;
00102 DWORD dw;
00103 if (ReadFile (hFile, &hdr, sizeof (hdr), &dw, NULL))
00104 {
00105 if (hdr.wVer == DLDSGRPSFILE_CURRENT_VERSION &&
00106 lstrcmp (hdr.szSig, DLDSGRPSFILE_SIG) == 0)
00107 {
00108 if (FALSE == ReadFile (hFile, &m_nGrpNextId, sizeof (UINT), &dw, NULL))
00109 return FALSE;
00110
00111 LoadGroupsTreeFromFile (hFile, &m_tGroups);
00112 }
00113 }
00114 CloseHandle (hFile);
00115 }
00116
00117 if (m_tGroups.GetLeafCount () == 0)
00118 CreateDefaultGroups ();
00119
00120 return TRUE;
00121 }
00122
00123 BOOL vmsDownloadsGroupsMgr::SaveToDisk()
00124 {
00125 fsString strFile = fsGetDataFilePath ("groups.sav");
00126
00127 HANDLE hFile = CreateFile (strFile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
00128 FILE_ATTRIBUTE_HIDDEN, NULL);
00129
00130 if (hFile == INVALID_HANDLE_VALUE)
00131 return FALSE;
00132
00133 vmsDownloadsGroupsFileHdr hdr;
00134 DWORD dw;
00135 if (FALSE == WriteFile (hFile, &hdr, sizeof (hdr), &dw, NULL))
00136 {
00137 CloseHandle (hFile);
00138 return FALSE;
00139 }
00140
00141 if (FALSE == WriteFile (hFile, &m_nGrpNextId, sizeof (UINT), &dw, NULL))
00142 {
00143 CloseHandle (hFile);
00144 return FALSE;
00145 }
00146
00147 BOOL bOk = SaveGroupsTreeToFile (hFile, &m_tGroups);
00148 CloseHandle (hFile);
00149 return bOk;
00150 }
00151
00152 PDLDS_GROUPS_TREE vmsDownloadsGroupsMgr::Add(vmsDownloadsGroupSmartPtr grp, vmsDownloadsGroupSmartPtr pParentGroup, BOOL bKeepIdAsIs)
00153 {
00154 PDLDS_GROUPS_TREE pParent = pParentGroup == NULL ? &m_tGroups : FindGroupInTree (pParentGroup);
00155 return Add (grp, pParent, bKeepIdAsIs);
00156 }
00157
00158 vmsDownloadsGroupSmartPtr vmsDownloadsGroupsMgr::FindGroup(UINT nId)
00159 {
00160 for (size_t i = 0; i < m_vGroups.size (); i++)
00161 {
00162 if (m_vGroups [i]->GetData ()->nId == nId)
00163 return m_vGroups [i]->GetData ();
00164 }
00165
00166 return NULL;
00167 }
00168
00169 fsString vmsDownloadsGroupsMgr::GetGroupFullName(UINT nId)
00170 {
00171 vmsDownloadsGroupSmartPtr pGroup = FindGroup (nId);
00172 if (pGroup == NULL)
00173 return "";
00174 PDLDS_GROUPS_TREE p = FindGroupInTree (pGroup);
00175 fsString strName;
00176
00177 while (p && p->GetData () != NULL)
00178 {
00179 fsString str = p->GetData ()->strName;
00180 if (strName.IsEmpty ())
00181 {
00182 strName = str;
00183 }
00184 else
00185 {
00186 str += '\\';
00187 str += strName;
00188 strName = str;
00189 }
00190
00191 p = p->GetRoot ();
00192 }
00193
00194 return strName;
00195 }
00196
00197 vmsDownloadsGroupSmartPtr vmsDownloadsGroupsMgr::FindGroupByExt(LPCSTR pszExt)
00198 {
00199 for (size_t i = 0; i < m_vGroups.size (); i++)
00200 {
00201 if (IsExtInExtsStr (m_vGroups [i]->GetData ()->strExts, pszExt))
00202 return m_vGroups [i]->GetData ();
00203 }
00204
00205 return NULL;
00206 }
00207
00208 size_t vmsDownloadsGroupsMgr::GetTotalCount()
00209 {
00210 return m_vGroups.size ();
00211 }
00212
00213 vmsDownloadsGroupSmartPtr vmsDownloadsGroupsMgr::GetGroup(size_t nIndex)
00214 {
00215 ASSERT (nIndex < m_vGroups.size ());
00216 return m_vGroups [nIndex]->GetData ();
00217 }
00218
00219 PDLDS_GROUPS_TREE vmsDownloadsGroupsMgr::GetGroupsTree()
00220 {
00221 return &m_tGroups;
00222 }
00223
00224 void vmsDownloadsGroupsMgr::GetGroupWithSubgroups(vmsDownloadsGroupSmartPtr pGroup, std::vector <vmsDownloadsGroupSmartPtr> &v)
00225 {
00226 PDLDS_GROUPS_TREE pGrp = FindGroupInTree (pGroup);
00227 if (pGrp != NULL)
00228 {
00229 v.push_back (pGrp->GetData ());
00230 GetSubgroups (pGrp, v);
00231 }
00232 }
00233
00234 void vmsDownloadsGroupsMgr::GetSubgroups(PDLDS_GROUPS_TREE pGroup, std::vector <vmsDownloadsGroupSmartPtr> &v)
00235 {
00236 for (int i = 0; i < pGroup->GetLeafCount (); i++)
00237 {
00238 v.push_back (pGroup->GetLeaf (i)->GetData ());
00239 GetSubgroups (pGroup->GetLeaf (i), v);
00240 }
00241 }
00242
00243 PDLDS_GROUPS_TREE vmsDownloadsGroupsMgr::FindGroupInTree(vmsDownloadsGroupSmartPtr pGroup)
00244 {
00245 for (size_t i = 0; i < m_vGroups.size (); i++)
00246 {
00247 if (pGroup->nId == m_vGroups [i]->GetData ()->nId)
00248 return m_vGroups [i];
00249 }
00250
00251 return NULL;
00252 }
00253
00254 fsString vmsDownloadsGroupsMgr::GetGroupsRootOutFolder()
00255 {
00256 return FindGroup (GRP_OTHER_ID)->strOutFolder;
00257 }
00258
00259 void vmsDownloadsGroupsMgr::SetGroupsRootOutFolder(LPCSTR psz)
00260 {
00261 SetGroupsRootOutFolder (GetGroupsTree (), psz);
00262 }
00263
00264 void vmsDownloadsGroupsMgr::SetGroupsRootOutFolder(PDLDS_GROUPS_TREE pRoot, LPCSTR pszFolder)
00265 {
00266 for (int i = 0; i < pRoot->GetLeafCount (); i++)
00267 {
00268 PDLDS_GROUPS_TREE pGroup = pRoot->GetLeaf (i);
00269
00270 fsString str = pszFolder;
00271 if (str [str.GetLength () - 1] != '\\' && str [str.GetLength () - 1] != '/')
00272 str += '\\';
00273 pGroup->GetData ()->strOutFolder = str;
00274
00275 if (pGroup->GetData ()->nId != GRP_OTHER_ID)
00276 {
00277 pGroup->GetData ()->strOutFolder += pGroup->GetData ()->strName;
00278 pGroup->GetData ()->strOutFolder += '\\';
00279 }
00280
00281 SetGroupsRootOutFolder (pGroup, pGroup->GetData ()->strOutFolder);
00282 }
00283 }
00284
00285 void vmsDownloadsGroupsMgr::DeleteGroup(vmsDownloadsGroupSmartPtr pGroup)
00286 {
00287 PDLDS_GROUPS_TREE pGrp = FindGroupInTree (pGroup);
00288 PDLDS_GROUPS_TREE pRoot = pGrp->GetRoot ();
00289 for (int i = 0; i < pRoot->GetLeafCount (); i++)
00290 {
00291 if (pRoot->GetLeaf (i) == pGrp)
00292 {
00293 pRoot->DeleteLeaf (i);
00294 RebuildGroupsList ();
00295 return;
00296 }
00297 }
00298 }
00299
00300 vmsDownloadsGroupSmartPtr vmsDownloadsGroupsMgr::FindGroupByName(LPCSTR pszName)
00301 {
00302 return FindGroupByName (pszName, GetGroupsTree ());
00303 }
00304
00305 BOOL vmsDownloadsGroupsMgr::LoadGroupsTreeFromFile(HANDLE hFile, PDLDS_GROUPS_TREE pRoot)
00306 {
00307 int cGroups = 0;
00308 DWORD dw;
00309 if (FALSE == ReadFile (hFile, &cGroups, sizeof (int), &dw, NULL))
00310 return FALSE;
00311
00312 while (cGroups-- > 0)
00313 {
00314 vmsDownloadsGroupSmartPtr pGroup;
00315 pGroup.CreateInstance ();
00316 if (FALSE == LoadGroupFromFile (hFile, pGroup))
00317 return FALSE;
00318 PDLDS_GROUPS_TREE pGroupRoot = Add (pGroup, pRoot, TRUE);
00319 if (FALSE == LoadGroupsTreeFromFile (hFile, pGroupRoot))
00320 return FALSE;
00321 }
00322
00323 return TRUE;
00324 }
00325
00326 BOOL vmsDownloadsGroupsMgr::LoadGroupFromFile(HANDLE hFile, vmsDownloadsGroupSmartPtr pGroup)
00327 {
00328 DWORD dw;
00329
00330 if (FALSE == ReadFile (hFile, &pGroup->nId, sizeof (pGroup->nId), &dw, NULL))
00331 return FALSE;
00332
00333 if (FALSE == fsReadStringFromFile (hFile, pGroup->strName))
00334 return FALSE;
00335
00336 if (FALSE == fsReadStringFromFile (hFile, pGroup->strOutFolder))
00337 return FALSE;
00338
00339 if (FALSE == fsReadStringFromFile (hFile, pGroup->strExts))
00340 return FALSE;
00341
00342 return TRUE;
00343 }
00344
00345 PDLDS_GROUPS_TREE vmsDownloadsGroupsMgr::Add(vmsDownloadsGroupSmartPtr grp, PDLDS_GROUPS_TREE pParentGroup, BOOL bKeepIdAsIs)
00346 {
00347 if (pParentGroup == NULL)
00348 pParentGroup = GetGroupsTree ();
00349
00350 if (bKeepIdAsIs == FALSE)
00351 grp->nId = m_nGrpNextId++;
00352 else
00353 m_nGrpNextId = max (m_nGrpNextId, grp->nId + 1);
00354
00355 grp->cDownloads = 0;
00356 grp->bAboutToBeDeleted = false;
00357
00358 PDLDS_GROUPS_TREE pGrp = pParentGroup->AddLeaf (grp);
00359 m_vGroups.push_back (pGrp);
00360
00361 return pGrp;
00362 }
00363
00364 vmsDownloadsGroupSmartPtr vmsDownloadsGroupsMgr::FindGroupByName(LPCSTR pszName, PDLDS_GROUPS_TREE pRoot)
00365 {
00366 fsString strName;
00367 while (*pszName && *pszName != '\\' && *pszName != '/')
00368 strName += *pszName++;
00369 if (*pszName)
00370 pszName++;
00371
00372 for (int i = 0; i < pRoot->GetLeafCount (); i++)
00373 {
00374 if (lstrcmpi (pRoot->GetLeaf (i)->GetData ()->strName, strName) == 0)
00375 {
00376 if (*pszName)
00377 return FindGroupByName (pszName, pRoot->GetLeaf (i));
00378 return pRoot->GetLeaf (i)->GetData ();
00379 }
00380 }
00381
00382 return NULL;
00383 }
00384
00385 BOOL vmsDownloadsGroupsMgr::SaveGroupsTreeToFile(HANDLE hFile, PDLDS_GROUPS_TREE pRoot)
00386 {
00387 int cGroups = pRoot->GetLeafCount ();
00388 DWORD dw;
00389 if (FALSE == WriteFile (hFile, &cGroups, sizeof (int), &dw, NULL))
00390 return FALSE;
00391
00392 for (int i = 0; i < cGroups; i++)
00393 {
00394 PDLDS_GROUPS_TREE pGroupTree = pRoot->GetLeaf (i);
00395 if (FALSE == SaveGroupToFile (hFile, pGroupTree->GetData ()))
00396 return FALSE;
00397 if (FALSE == SaveGroupsTreeToFile (hFile, pGroupTree))
00398 return FALSE;
00399 }
00400
00401 return TRUE;
00402 }
00403
00404 BOOL vmsDownloadsGroupsMgr::SaveGroupToFile(HANDLE hFile, vmsDownloadsGroupSmartPtr pGroup)
00405 {
00406 DWORD dw;
00407
00408 if (FALSE == WriteFile (hFile, &pGroup->nId, sizeof (pGroup->nId), &dw, NULL))
00409 return FALSE;
00410
00411 if (FALSE == fsSaveStrToFile (pGroup->strName, hFile))
00412 return FALSE;
00413
00414 if (FALSE == fsSaveStrToFile (pGroup->strOutFolder, hFile))
00415 return FALSE;
00416
00417 if (FALSE == fsSaveStrToFile (pGroup->strExts, hFile))
00418 return FALSE;
00419
00420 return TRUE;
00421 }
00422
00423 LPCSTR vmsDownloadsGroupsMgr::GetVideoExts()
00424 {
00425 return "avi mpg mov wmv mpeg vob mpe flv mp4";
00426 }
00427
00428 LPCSTR vmsDownloadsGroupsMgr::GetAudioExts()
00429 {
00430 return "mp3 wav au ogg aif aiff snd voc aac mid wma";
00431 }
00432
00433 void vmsDownloadsGroupsMgr::RebuildGroupsList()
00434 {
00435 std::vector <PDLDS_GROUPS_TREE> v;
00436 RebuildGroupsList (&m_tGroups, v);
00437 m_vGroups = v;
00438 }
00439
00440 void vmsDownloadsGroupsMgr::RebuildGroupsList(PDLDS_GROUPS_TREE pRoot, std::vector <PDLDS_GROUPS_TREE> &v)
00441 {
00442 for (int i = 0; i < pRoot->GetLeafCount (); i++)
00443 {
00444 PDLDS_GROUPS_TREE pGroupTree = pRoot->GetLeaf (i);
00445 v.push_back (pGroupTree);
00446 RebuildGroupsList (pGroupTree, v);
00447 }
00448 }