no message
[xbmc:xbmc-antiquated.git] / XBMC / xbmc / GUIWindowPrograms.cpp
1 #include "guiwindowprograms.h"
2 #include "localizestrings.h"
3 #include "GUIWindowManager.h" 
4 #include "util.h"
5 #include "Xbox\IoSupport.h"
6 #include "Xbox\Undocumented.h"
7 #include "crc32.h"
8 #include "settings.h"
9 #include "lib/cximage/ximage.h"
10 #include "Shortcut.h"
11 #include "guidialog.h"
12 #include "sectionLoader.h"
13 #include "application.h"
14 #include "filesystem/HDDirectory.h"
15 #include <algorithm>
16
17 using namespace DIRECTORY;
18 #define CONTROL_BTNVIEWAS     2
19 #define CONTROL_BTNSCAN             3
20 #define CONTROL_BTNSORTMETHOD 4
21 #define CONTROL_BTNSORTASC    5
22 #define CONTROL_LIST            7
23 #define CONTROL_THUMBS        8
24 #define CONTROL_LABELFILES    9
25
26 CGUIWindowPrograms::CGUIWindowPrograms(void)
27 :CGUIWindow(0)
28 {
29   m_strDirectory="";
30   
31 }
32
33 CGUIWindowPrograms::~CGUIWindowPrograms(void)
34 {
35 }
36
37 bool CGUIWindowPrograms::OnMessage(CGUIMessage& message)
38 {
39   switch ( message.GetMessage() )
40   {
41     case GUI_MSG_WINDOW_DEINIT:
42       Clear();
43     break;
44
45     case GUI_MSG_WINDOW_INIT:
46     {
47                         m_dlgProgress = (CGUIDialogProgress*)m_gWindowManager.GetWindow(101);
48                         CUtil::GetHomePath(m_strDirectory);
49                         m_strDirectory+="\\shortcuts";
50       // make controls 100-110 invisible...
51                         for (int i=100; i < 110; i++)
52       {
53                                 SET_CONTROL_HIDDEN(GetID(), i);
54       }
55                         // create bookmark buttons
56       int iStartID=100;
57       
58                         for (i=0; i < (int)g_settings.m_vecMyProgramsBookmarks.size(); ++i)
59                         {
60                                 CShare& share = g_settings.m_vecMyProgramsBookmarks[i];
61                                 
62                                 SET_CONTROL_VISIBLE(GetID(), i+iStartID);
63                                 SET_CONTROL_LABEL(GetID(), i+iStartID,share.strName);
64                         }
65
66       
67       Update(m_strDirectory);
68     }
69     break;
70
71     case GUI_MSG_SETFOCUS:
72     {
73     }
74     break;
75
76     case GUI_MSG_CLICKED:
77     {
78       int iControl=message.GetSenderId();
79       if (iControl==CONTROL_BTNVIEWAS)
80       {
81                                 //CGUIDialog* pDialog=(CGUIDialog*)m_gWindowManager.GetWindow(100);
82                                 //pDialog->DoModal(GetID());
83
84         g_stSettings.m_bMyProgramsViewAsIcons=!g_stSettings.m_bMyProgramsViewAsIcons;
85                                 g_settings.Save();
86                                 
87         UpdateButtons();
88       }
89       else if (iControl==CONTROL_BTNSCAN) // button
90       {
91                                 int iTotalApps=0;
92                                 CUtil::GetHomePath(m_strDirectory);
93                                 m_strDirectory+="\\shortcuts";
94                                 
95                                 CHDDirectory rootDir;
96                                 Clear();
97                                 OnSort();
98                                 UpdateButtons();
99         
100                                 // remove shortcuts...
101                                 rootDir.SetMask(".cut");
102                                 rootDir.GetDirectory(m_strDirectory,m_vecItems);
103                                 for (int i=0; i < (int)m_vecItems.size(); ++i)
104                                 {
105                                         CFileItem* pItem=m_vecItems[i];
106                                         if (CUtil::IsShortCut(pItem->m_strPath))
107                                         {
108                                                 DeleteFile(pItem->m_strPath.c_str());
109                                         }
110                                 }
111
112                                 // create new ones.
113                                 Clear();
114                                 rootDir.SetMask(".xbe");
115                                 rootDir.GetDirectory("C:\\",m_vecItems);
116         OnScan(m_vecItems,iTotalApps);
117                                 Clear();
118                                 rootDir.GetDirectory("E:\\",m_vecItems);
119         OnScan(m_vecItems,iTotalApps);
120                                 Clear();
121                                 rootDir.GetDirectory("F:\\",m_vecItems);
122         OnScan(m_vecItems,iTotalApps);
123                                 Clear();
124
125                                 Update(m_strDirectory);
126       }
127       else if (iControl==CONTROL_BTNSORTMETHOD) // sort by
128       {
129         g_stSettings.m_bMyProgramsSortMethod++;
130         if (g_stSettings.m_bMyProgramsSortMethod >=3) g_stSettings.m_bMyProgramsSortMethod=0;
131                                 g_settings.Save();
132         UpdateButtons();
133         OnSort();
134       }
135       else if (iControl==CONTROL_BTNSORTASC) // sort asc
136       {
137         g_stSettings.m_bMyProgramsSortAscending=!g_stSettings.m_bMyProgramsSortAscending;
138                                 g_settings.Save();
139         
140         UpdateButtons();
141         OnSort();
142       }
143       else if (iControl==CONTROL_LIST||iControl==CONTROL_THUMBS)  // list/thumb control
144       {
145          // get selected item
146         CGUIMessage msg(GUI_MSG_ITEM_SELECTED,GetID(),iControl,0,0,NULL);
147         g_graphicsContext.SendMessage(msg);         
148         int iItem=msg.GetParam1();
149         OnClick(iItem);
150         
151       }
152                         else if (iControl >= 100 && iControl <= 110)
153                         {
154                                 // bookmark button
155                                 int iShare=iControl-100;
156                                 if (iShare < (int)g_settings.m_vecMyProgramsBookmarks.size())
157                                 {
158                                         CShare share = g_settings.m_vecMyProgramsBookmarks[iControl-100];
159                                         m_strDirectory=share.strPath;
160                                         Update(m_strDirectory);
161                                 }
162                         }
163     }
164   }
165
166   return CGUIWindow::OnMessage(message);
167 }
168
169 void CGUIWindowPrograms::Render()
170 {
171   CGUIWindow::Render();
172 }
173
174
175 void CGUIWindowPrograms::OnKey(const CKey& key)
176 {
177   if (key.IsButton())
178   {
179     if ( key.GetButtonCode() == KEY_BUTTON_BACK  || key.GetButtonCode() == KEY_REMOTE_BACK)
180     {
181       m_gWindowManager.ActivateWindow(0); // back 2 home
182       return;
183     }
184   }
185   CGUIWindow::OnKey(key);
186 }
187
188
189 void CGUIWindowPrograms::LoadDirectory(const CStdString& strDirectory)
190 {
191         WIN32_FIND_DATA wfd;
192         HANDLE hFind;
193   bool   bRecurseSubDirs(true);
194         memset(&wfd,0,sizeof(wfd));
195         CStdString strRootDir=strDirectory;
196         if (!CUtil::HasSlashAtEnd(strRootDir) )
197                 strRootDir+="\\";
198
199         if ( CUtil::IsDVD(strRootDir) )
200   {
201     CIoSupport helper;
202     helper.Remount("D:","Cdrom0");
203   }
204   CStdString strSearchMask=strRootDir;
205   strSearchMask+="*.*";
206
207   FILETIME localTime;
208   hFind = FindFirstFile(strSearchMask.c_str(),&wfd);
209         if (hFind==NULL)
210                 return ;
211         do
212         {
213                 if (wfd.cFileName[0]!=0)
214                 {
215       CStdString strFileName=wfd.cFileName;
216       CStdString strFile=strRootDir;
217       strFile+=wfd.cFileName;
218
219                         if ( (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
220       {
221         if (strFileName != "." && strFileName != "..")
222         {
223           CFileItem *pItem = new CFileItem(strFileName);
224           pItem->m_strPath=strFile;
225           pItem->m_bIsFolder=true;
226           FileTimeToLocalFileTime(&wfd.ftLastWriteTime,&localTime);
227           FileTimeToSystemTime(&localTime, &pItem->m_stTime);
228         
229           m_vecItems.push_back(pItem);      
230         }
231       }
232       else
233       {
234         if (CUtil::IsXBE(strFileName))
235         {
236           CStdString strDescription;
237                                         CUtil::GetXBEDescription(strFile,strDescription);
238           CFileItem *pItem = new CFileItem(strDescription);
239           pItem->m_strPath=strFile;
240           pItem->m_bIsFolder=false;
241           pItem->m_dwSize=wfd.nFileSizeLow;
242
243           FileTimeToLocalFileTime(&wfd.ftLastWriteTime,&localTime);
244           FileTimeToSystemTime(&localTime, &pItem->m_stTime);
245           m_vecItems.push_back(pItem);      
246           bRecurseSubDirs=false;          
247         }
248
249         if ( CUtil::IsShortCut(strFileName)  )
250         {
251           CFileItem *pItem = new CFileItem(wfd.cFileName);
252           pItem->m_strPath=strFile;
253           pItem->m_bIsFolder=false;
254           pItem->m_dwSize=wfd.nFileSizeLow;
255
256
257           FileTimeToLocalFileTime(&wfd.ftLastWriteTime,&localTime);
258           FileTimeToSystemTime(&localTime, &pItem->m_stTime);
259           m_vecItems.push_back(pItem);      
260         }
261       }
262     }
263   } while (FindNextFile(hFind, &wfd));
264
265         FindClose( hFind );       
266
267         CUtil::SetThumbs(m_vecItems);
268         CUtil::FillInDefaultIcons(m_vecItems);
269 }
270
271 void CGUIWindowPrograms::Clear()
272 {
273   for (int i=0; i < (int)m_vecItems.size(); i++)
274   {
275     CFileItem* pItem=m_vecItems[i];
276     delete pItem;
277   }
278    m_vecItems.erase(m_vecItems.begin(),m_vecItems.end() );
279 }
280
281 void CGUIWindowPrograms::Update(const CStdString &strDirectory)
282 {
283   Clear();
284  
285   CStdString strParentPath;
286   if (CUtil::GetParentPath(strDirectory,strParentPath))
287   {
288     CFileItem *pItem = new CFileItem("..");
289     pItem->m_strPath=strParentPath;
290     pItem->m_bIsShareOrDrive=false;
291     pItem->m_bIsFolder=true;
292     m_vecItems.push_back(pItem);
293   }
294  
295   LoadDirectory(strDirectory);
296   OnSort();
297   UpdateButtons();
298 }
299
300 void CGUIWindowPrograms::OnClick(int iItem)
301 {
302   CFileItem* pItem=m_vecItems[iItem];
303   if (pItem->m_bIsFolder)
304   {
305     m_strDirectory=pItem->m_strPath;
306                 m_strDirectory+="\\";
307     Update(m_strDirectory);
308   }
309   else
310   {
311     // launch xbe...
312     char szPath[1024];
313     char szDevicePath[1024];
314     char szXbePath[1024];
315           char szParameters[1024];
316     memset(szParameters,0,sizeof(szParameters));
317     strcpy(szPath,pItem->m_strPath.c_str());
318
319     if (CUtil::IsShortCut(pItem->m_strPath) )
320     {
321       CShortcut shortcut;
322                   if ( shortcut.Create(pItem->m_strPath))
323                   {
324                           // if another shortcut is specified, load this up and use it
325                           if ( CUtil::IsShortCut(shortcut.m_strPath.c_str() ) )
326                           {
327                                   CHAR szNewPath[1024];
328                                   strcpy(szNewPath,szPath);
329                                   CHAR* szFile = strrchr(szNewPath,'\\');
330                                   strcpy(&szFile[1],shortcut.m_strPath.c_str());
331
332                                   CShortcut targetShortcut;
333                                   if (FAILED(targetShortcut.Create(szNewPath)))
334                                           return;
335
336                                   shortcut.m_strPath = targetShortcut.m_strPath;
337                           }
338
339                           strcpy( szPath, shortcut.m_strPath.c_str() );
340                           strcpy( szDevicePath, shortcut.m_strPath.c_str() );
341
342                           CHAR* szXbe = strrchr(szDevicePath,'\\');
343                           *szXbe++=0x00;
344
345                           wsprintf(szXbePath,"d:\\%s",szXbe);
346
347                           CHAR szMode[16];
348                                 strcpy( szMode, shortcut.m_strVideo.c_str() );
349                                 strlwr( szMode );
350
351                                 LPDWORD pdwVideo = (LPDWORD) 0x8005E760;
352                                 BOOL    bRow = strstr(szMode,"pal")!=NULL;
353                                 BOOL    bJap = strstr(szMode,"ntsc-j")!=NULL;
354                                 BOOL    bUsa = strstr(szMode,"ntsc")!=NULL;
355
356                                 if (bRow)
357                                         *pdwVideo = 0x00800300;
358                                 else if (bJap)
359                                         *pdwVideo = 0x00400200;
360                                 else if (bUsa)
361                                         *pdwVideo = 0x00400100;
362                           
363                           strcat(szParameters, shortcut.m_strParameters.c_str());
364                   }
365           }
366     char* szBackslash = strrchr(szPath,'\\');
367                 *szBackslash=0x00;
368                 char* szXbe = &szBackslash[1];
369
370                 char* szColon = strrchr(szPath,':');
371                 *szColon=0x00;
372                 char* szDrive = szPath;
373                 char* szDirectory = &szColon[1];
374     
375     CIoSupport helper;
376                 helper.GetPartition( (LPCSTR) szDrive, szDevicePath);
377
378                 strcat(szDevicePath,szDirectory);
379                 wsprintf(szXbePath,"d:\\%s",szXbe);
380
381
382                 m_gWindowManager.DeInitialize();
383                 CSectionLoader::UnloadAll();
384
385                 g_application.Stop();
386     if (strlen(szParameters))
387       CUtil::LaunchXbe(szDevicePath,szXbePath,szParameters);
388     else
389       CUtil::LaunchXbe(szDevicePath,szXbePath,NULL);
390   }
391 }
392
393 struct SSortProgramsByName
394 {
395         bool operator()(CFileItem* pStart, CFileItem* pEnd)
396         {
397     CFileItem& rpStart=*pStart;
398     CFileItem& rpEnd=*pEnd;
399                 if (rpStart.GetLabel()=="..") return true;
400                 if (rpEnd.GetLabel()=="..") return false;
401                 bool bGreater=true;
402                 if (g_stSettings.m_bMyProgramsSortAscending) bGreater=false;
403     if ( rpStart.m_bIsFolder   == rpEnd.m_bIsFolder)
404                 {
405                         char szfilename1[1024];
406                         char szfilename2[1024];
407
408                         switch ( g_stSettings.m_bMyProgramsSortMethod ) 
409                         {
410                                 case 0: //      Sort by Filename
411           strcpy(szfilename1, rpStart.m_strPath.c_str());
412                                         strcpy(szfilename2, rpEnd.m_strPath.c_str());
413                                         break;
414                                 case 1: // Sort by Date
415           if ( rpStart.m_stTime.wYear > rpEnd.m_stTime.wYear ) return bGreater;
416                                         if ( rpStart.m_stTime.wYear < rpEnd.m_stTime.wYear ) return !bGreater;
417                                         
418                                         if ( rpStart.m_stTime.wMonth > rpEnd.m_stTime.wMonth ) return bGreater;
419                                         if ( rpStart.m_stTime.wMonth < rpEnd.m_stTime.wMonth ) return !bGreater;
420                                         
421                                         if ( rpStart.m_stTime.wDay > rpEnd.m_stTime.wDay ) return bGreater;
422                                         if ( rpStart.m_stTime.wDay < rpEnd.m_stTime.wDay ) return !bGreater;
423
424                                         if ( rpStart.m_stTime.wHour > rpEnd.m_stTime.wHour ) return bGreater;
425                                         if ( rpStart.m_stTime.wHour < rpEnd.m_stTime.wHour ) return !bGreater;
426
427                                         if ( rpStart.m_stTime.wMinute > rpEnd.m_stTime.wMinute ) return bGreater;
428                                         if ( rpStart.m_stTime.wMinute < rpEnd.m_stTime.wMinute ) return !bGreater;
429
430                                         if ( rpStart.m_stTime.wSecond > rpEnd.m_stTime.wSecond ) return bGreater;
431                                         if ( rpStart.m_stTime.wSecond < rpEnd.m_stTime.wSecond ) return !bGreater;
432                                         return true;
433                                 break;
434
435         case 2:
436           if ( rpStart.m_dwSize > rpEnd.m_dwSize) return bGreater;
437                                         if ( rpStart.m_dwSize < rpEnd.m_dwSize) return !bGreater;
438                                         return true;
439         break;
440
441                                 default:        //      Sort by Filename by default
442                                         strcpy(szfilename1, rpStart.GetLabel().c_str());
443                                         strcpy(szfilename2, rpEnd.GetLabel().c_str());
444                                         break;
445                         }
446
447
448                         for (int i=0; i < (int)strlen(szfilename1); i++)
449                                 szfilename1[i]=tolower((unsigned char)szfilename1[i]);
450                         
451                         for (i=0; i < (int)strlen(szfilename2); i++)
452                                 szfilename2[i]=tolower((unsigned char)szfilename2[i]);
453                         //return (rpStart.strPath.compare( rpEnd.strPath )<0);
454
455                         if (g_stSettings.m_bMyProgramsSortAscending)
456                                 return (strcmp(szfilename1,szfilename2)<0);
457                         else
458                                 return (strcmp(szfilename1,szfilename2)>=0);
459                 }
460     if (!rpStart.m_bIsFolder) return false;
461                 return true;
462         }
463 };
464
465 void CGUIWindowPrograms::OnSort()
466 {
467   CGUIMessage msg(GUI_MSG_LABEL_RESET,GetID(),CONTROL_LIST,0,0,NULL);
468   g_graphicsContext.SendMessage(msg);         
469
470   
471   CGUIMessage msg2(GUI_MSG_LABEL_RESET,GetID(),CONTROL_THUMBS,0,0,NULL);
472   g_graphicsContext.SendMessage(msg2);         
473
474   
475   
476   for (int i=0; i < (int)m_vecItems.size(); i++)
477   {
478     CFileItem* pItem=m_vecItems[i];
479     if (g_stSettings.m_bMyProgramsSortMethod==0||g_stSettings.m_bMyProgramsSortMethod==2)
480     {
481                         if (pItem->m_bIsFolder) pItem->SetLabel2("");
482       else 
483                         {
484                                 CStdString strFileSize;
485                                 CUtil::GetFileSize(pItem->m_dwSize, strFileSize);
486                                 pItem->SetLabel2(strFileSize);
487                         }
488     }
489     else
490     {      
491       if (pItem->m_stTime.wYear)
492                         {
493                                 CStdString strDateTime;
494         CUtil::GetDate(pItem->m_stTime, strDateTime);
495         pItem->SetLabel2(strDateTime);
496                         }
497       else
498         pItem->SetLabel2("");
499     }
500   }
501
502   
503   sort(m_vecItems.begin(), m_vecItems.end(), SSortProgramsByName());
504
505   for (int i=0; i < (int)m_vecItems.size(); i++)
506   {
507     CFileItem* pItem=m_vecItems[i];
508     CGUIMessage msg(GUI_MSG_LABEL_ADD,GetID(),CONTROL_LIST,0,0,(void*)pItem);
509     g_graphicsContext.SendMessage(msg);    
510     CGUIMessage msg2(GUI_MSG_LABEL_ADD,GetID(),CONTROL_THUMBS,0,0,(void*)pItem);
511     g_graphicsContext.SendMessage(msg2);         
512   }
513 }
514
515
516
517
518
519 void CGUIWindowPrograms::UpdateButtons()
520 {
521     if (g_stSettings.m_bMyProgramsViewAsIcons) 
522     {
523                         SET_CONTROL_HIDDEN(GetID(), CONTROL_LIST);
524                         SET_CONTROL_VISIBLE(GetID(), CONTROL_THUMBS);
525     }
526     else
527     {
528                         SET_CONTROL_HIDDEN(GetID(), CONTROL_THUMBS);
529       SET_CONTROL_VISIBLE(GetID(), CONTROL_LIST);
530     }
531
532     const WCHAR *szText;
533                 int iString=101;
534     if (!g_stSettings.m_bMyProgramsViewAsIcons) 
535     {
536       iString=100;
537     }
538                 SET_CONTROL_LABEL(GetID(), CONTROL_BTNVIEWAS,iString);
539                 SET_CONTROL_LABEL(GetID(), CONTROL_BTNSORTMETHOD,g_stSettings.m_bMyProgramsSortMethod+103);
540
541
542     if ( g_stSettings.m_bMyProgramsSortAscending)
543     {
544       CGUIMessage msg(GUI_MSG_DESELECTED,GetID(), CONTROL_BTNSORTASC);
545       g_graphicsContext.SendMessage(msg);
546     }
547     else
548     {
549       CGUIMessage msg(GUI_MSG_SELECTED,GetID(), CONTROL_BTNSORTASC);
550       g_graphicsContext.SendMessage(msg);
551     }
552
553     int iItems=m_vecItems.size();
554     if (iItems)
555     {
556       CFileItem* pItem=m_vecItems[0];
557       if (pItem->GetLabel() =="..") iItems--;
558     }
559     WCHAR wszText[20];
560     szText=g_localizeStrings.Get(127).c_str();
561     swprintf(wszText,L"%i %s", iItems,szText);
562     
563                 SET_CONTROL_LABEL(GetID(), CONTROL_LABELFILES,wszText);
564     
565
566 }
567
568 void CGUIWindowPrograms::OnScan(VECFILEITEMS& items, int& iTotalAppsFound)
569 {
570         const WCHAR* pszText=(WCHAR*)g_localizeStrings.Get(212).c_str();
571         WCHAR wzTotal[128];
572         swprintf(wzTotal,L"%i %s",iTotalAppsFound, pszText );
573         m_dlgProgress->SetHeading(211);
574         m_dlgProgress->SetLine(0,wzTotal);
575         m_dlgProgress->SetLine(1,"");
576         m_dlgProgress->SetLine(2,m_strDirectory );
577         m_dlgProgress->StartModal(GetID());
578         m_dlgProgress->Progress();
579
580         bool bFound=false;
581         CUtil::SetThumbs(items);
582         bool bOpen=true;        
583         for (int i=0; i < (int)items.size(); ++i)
584         {
585                 CFileItem *pItem= items[i];
586                 if ( pItem->m_bIsFolder)
587                 {
588                         if (!bFound && pItem->GetLabel() != "..")
589                         {
590                                 // load subfolder
591                                 CStdString strDir=m_strDirectory;
592                                 if (pItem->m_strPath != "E:\\UDATA" && pItem->m_strPath !="E:\\TDATA") 
593                                 {
594                                         m_strDirectory=pItem->m_strPath;
595                                         VECFILEITEMS subDirItems;
596                                         CHDDirectory rootDir;
597                                         rootDir.SetMask(".xbe");
598                                         rootDir.GetDirectory(pItem->m_strPath,subDirItems);
599                                         bOpen=false;    
600                                         m_dlgProgress->Close();
601                                         OnScan(subDirItems,iTotalAppsFound);
602                                         for (int x=0; x < (int)subDirItems.size(); ++x)
603                                         {
604                                                 CFileItem *pSubItem= subDirItems[x];
605                                                 delete pSubItem;
606                                         }
607                                         m_strDirectory=strDir;
608                                 }
609                         }
610                 }
611                 else
612                 {
613                         if (CUtil::IsXBE(pItem->m_strPath) )
614                         {
615                                 bFound=true;
616                                 CStdString strTotal;
617                                 iTotalAppsFound++;
618                                 
619                                 swprintf(wzTotal,L"%i %s",iTotalAppsFound, pszText );
620                                 CStdString strDescription;
621                                 CUtil::GetXBEDescription(pItem->m_strPath,strDescription);
622                                 if (strDescription="")
623                                         strDescription=CUtil::GetFileName(pItem->m_strPath);
624                                 m_dlgProgress->SetLine(0, wzTotal);
625                                 m_dlgProgress->SetLine(1,strDescription);
626                                 m_dlgProgress->Progress();
627                         }
628                 }
629         }
630         if (bOpen)
631                 m_dlgProgress->Close();
632 }