dont show music overlay info on some screens
[xbmc:xbmc-antiquated.git] / xbmc / Util.cpp
1 #include "util.h"
2 #include "xbox/iosupport.h"
3 #include "crc32.h"
4 #include "settings.h"
5 #include "xbox/undocumented.h"
6 #include "lib/common/xbnet.h"
7 #include "url.h"
8 #include "shortcut.h"
9 #include "common/xbresource.h"
10 #include "graphiccontext.h"
11 #include "sectionloader.h"
12 #include "lib/cximage/ximage.h"
13 #include "filesystem/file.h"
14
15 using namespace XFILE;
16 char g_szTitleIP[32];
17
18 CUtil::CUtil(void)
19 {
20   memset(g_szTitleIP,0,sizeof(g_szTitleIP));
21 }
22
23 CUtil::~CUtil(void)
24 {
25 }
26
27 char* CUtil::GetExtension(const CStdString& strFileName) 
28 {
29   char* extension = strrchr(strFileName.c_str(),'.');
30   return extension ;
31 }
32
33 bool CUtil::IsXBE(const CStdString& strFileName)
34 {
35    char* pExtension=GetExtension(strFileName);
36    if (!pExtension) return false;
37    if (CUtil::cmpnocase(pExtension,".xbe")==0) return true;
38    return false;
39 }
40
41 bool CUtil::IsShortCut(const CStdString& strFileName)
42 {
43    char* pExtension=GetExtension(strFileName);
44    if (!pExtension) return false;
45    if (CUtil::cmpnocase(pExtension,".cut")==0) return true;
46    return false;
47 }
48
49 int CUtil::cmpnocase(const char* str1,const char* str2)
50 {
51         int iLen;
52         if ( strlen(str1) != strlen(str2) ) return 1;
53         
54         iLen=strlen(str1);
55         for (int i=0; i < iLen;i++ )
56         {
57                 if (tolower((unsigned char)str1[i]) != tolower((unsigned char)str2[i]) ) return 1;
58         }
59         return 0;
60 }
61
62
63 char* CUtil::GetFileName(const CStdString& strFileNameAndPath)
64 {
65
66   char* extension = strrchr(strFileNameAndPath.c_str(),'\\');
67   if (!extension)
68   {
69     extension = strrchr(strFileNameAndPath.c_str(),'/');
70     if (!extension) return (char*)strFileNameAndPath.c_str();
71   }
72
73   extension++;
74   return extension;
75
76 }
77
78
79 bool CUtil::GetParentPath(const CStdString& strPath, CStdString& strParent)
80 {
81         strParent="";
82
83         CURL url(strPath);
84         CStdString strFile=url.GetFileName();
85         if (strFile.size()==0) return false;
86
87         if (HasSlashAtEnd(strFile) )
88         {
89                 strFile=strFile.Left(strFile.size()-1);
90         }
91
92         int iPos=strFile.ReverseFind('/');
93         if (iPos < 0)
94         {
95                 iPos=strFile.ReverseFind('\\');
96         }
97         if (iPos < 0)
98         {
99                 url.SetFileName("");
100                 url.GetURL(strParent);
101                 return true;
102         }
103
104         strFile=strFile.Left(iPos);
105         url.SetFileName(strFile);
106         url.GetURL(strParent);
107   return true;
108 }
109
110 //*********************************************************************************************
111 void CUtil::LaunchXbe(char* szPath, char* szXbe, char* szParameters)
112 {
113         OutputDebugString("Mounting ");
114         OutputDebugString(szPath);
115         OutputDebugString(" as D:\n");
116
117         CIoSupport helper;
118         helper.Unmount("D:");
119         helper.Mount("D:",szPath);
120
121         OutputDebugString("Launching ");
122         OutputDebugString( (szXbe!=NULL) ? szXbe:"default title" );
123         OutputDebugString("\n");                                
124
125         if (szParameters==NULL)
126         {
127                 XLaunchNewImage(szXbe, NULL );
128         }
129         else
130         {
131                 LAUNCH_DATA LaunchData;
132                 strcpy((char*)LaunchData.Data,szParameters);
133
134                 XLaunchNewImage(szXbe, &LaunchData );
135         }
136 }
137
138 bool CUtil::FileExists(const CStdString& strFileName)
139 {
140         if (strFileName.size()==0) return false;
141   FILE *fd;
142   fd=fopen(strFileName.c_str(),"rb");
143   if (fd != NULL)
144   {
145     fclose(fd);
146     return true;
147   }
148   return false;
149 }
150
151 void CUtil::GetThumbnail(const CStdString& strFileName, CStdString& strThumb)
152 {
153         if (CUtil::IsXBE(strFileName))
154         {
155                 if (CUtil::GetXBEIcon(strFileName,strThumb) ) return ;
156                 strThumb="defaultProgamIcon.png";
157                 return;
158         }
159
160         if (CUtil::IsShortCut(strFileName) )
161         {
162                 CShortcut shortcut;
163                 if ( shortcut.Create( strFileName ) )
164                 {
165                         CStdString strFile=shortcut.m_strPath;
166                         
167                         GetThumbnail(strFile,strThumb);
168                         return;                 
169                 }
170         }
171
172   char szThumbNail[1024];
173         Crc32 crc;
174         crc.Reset();
175   crc.Compute(strFileName.c_str(),strlen(strFileName.c_str()));
176   sprintf(szThumbNail,"%s\\%x.tbn",g_stSettings.szThumbnailsDirectory,crc);
177   strThumb= szThumbNail;
178 }
179
180 void CUtil::GetFileSize(DWORD dwFileSize, CStdString& strFileSize)
181 {
182   char szTemp[128];
183   if (dwFileSize < 1024)
184   {
185     sprintf(szTemp,"%i", dwFileSize);
186     strFileSize=szTemp;
187     return;
188   }
189   if (dwFileSize < 1024*1024)
190   {
191     sprintf(szTemp,"%02.1f KB", ((float)dwFileSize)/1024.0f);
192     strFileSize=szTemp;
193     return;
194   }
195   sprintf(szTemp,"%02.1f MB", ((float)dwFileSize)/(1024.0f*1024.0f));
196   strFileSize=szTemp;
197   return;
198   
199 }
200
201 void CUtil::GetDate(SYSTEMTIME stTime, CStdString& strDateTime)
202 {
203   char szTmp[128];
204   sprintf(szTmp,"%i-%i-%i %02.2i:%02.2i",
205           stTime.wDay,stTime.wMonth,stTime.wYear,
206           stTime.wHour,stTime.wMinute);
207   strDateTime=szTmp;
208 }
209
210 void CUtil::GetHomePath(CStdString& strPath)
211 {
212         char szXBEFileName[1024];
213   CIoSupport helper;
214   helper.GetXbePath(szXBEFileName);
215   char *szFileName = strrchr(szXBEFileName,'\\');
216   *szFileName=0;
217   strPath=szXBEFileName;
218 }
219
220 bool CUtil::IsEthernetConnected()
221 {
222         if (!(XNetGetEthernetLinkStatus() & XNET_ETHERNET_LINK_ACTIVE))
223                 return false;
224
225         return true;
226 }
227
228 bool CUtil::InitializeNetwork(const char* szLocalAddress, const char* szLocalSubnet, const char* szLocalGateway)
229 {
230         if (!IsEthernetConnected())
231                 return false;
232
233         // if local address is specified
234         if ( (szLocalAddress[0]!=0) &&
235                  (szLocalSubnet[0]!=0)  &&
236                  (szLocalGateway[0]!=0)  )
237
238         {
239                 // Thanks and credits to Team Evox for the description of the 
240                 // XNetConfigParams structure.
241
242                 TXNetConfigParams configParams;   
243
244                 OutputDebugString("Loading network configuration...\n");
245                 XNetLoadConfigParams( (LPBYTE) &configParams );
246                 OutputDebugString("Ready.\n");
247
248                 BOOL bXboxVersion2 = (configParams.V2_Tag == 0x58425632 );      // "XBV2"
249                 BOOL bDirty = false;
250
251                 OutputDebugString("User local address: ");
252                 OutputDebugString(szLocalAddress);
253                 OutputDebugString("\n");
254         
255                 if (bXboxVersion2)
256                 {
257                         if (configParams.V2_IP != inet_addr(szLocalAddress))
258                         {
259                                 configParams.V2_IP = inet_addr(szLocalAddress);
260                                 bDirty = true;
261                         }
262                 }
263                 else
264                 {
265                         if (configParams.V1_IP != inet_addr(szLocalAddress))
266                         {
267                                 configParams.V1_IP = inet_addr(szLocalAddress);
268                                 bDirty = true;
269                         }
270                 }
271
272                 OutputDebugString("User subnet mask: ");
273                 OutputDebugString(szLocalSubnet);
274                 OutputDebugString("\n");
275
276                 if (bXboxVersion2)
277                 {
278                         if (configParams.V2_Subnetmask != inet_addr(szLocalSubnet))
279                         {
280                                 configParams.V2_Subnetmask = inet_addr(szLocalSubnet);
281                                 bDirty = true;
282                         }
283                 }
284                 else
285                 {
286                         if (configParams.V1_Subnetmask != inet_addr(szLocalSubnet))
287                         {
288                                 configParams.V1_Subnetmask = inet_addr(szLocalSubnet);
289                                 bDirty = true;
290                         }
291                 }
292
293                 OutputDebugString("User gateway address: ");
294                 OutputDebugString(szLocalGateway);
295                 OutputDebugString("\n");
296
297                 if (bXboxVersion2)
298                 {
299                         if (configParams.V2_Defaultgateway != inet_addr(szLocalGateway))
300                         {
301                                 configParams.V2_Defaultgateway = inet_addr(szLocalGateway);
302                                 bDirty = true;
303                         }
304                 }
305                 else
306                 {
307                         if (configParams.V1_Defaultgateway != inet_addr(szLocalGateway))
308                         {
309                                 configParams.V1_Defaultgateway = inet_addr(szLocalGateway);
310                                 bDirty = true;
311                         }
312                 }
313
314                 if (configParams.Flag != (0x04|0x08) )
315                 {
316                         configParams.Flag = 0x04 | 0x08;
317                         bDirty = true;
318                 }
319
320                 if (bDirty)
321                 {
322                         OutputDebugString("Updating network configuration...\n");
323                         XNetSaveConfigParams( (LPBYTE) &configParams );
324                         OutputDebugString("Ready.\n");
325                 }
326         }
327
328         XNetStartupParams xnsp;
329         memset(&xnsp, 0, sizeof(xnsp));
330         xnsp.cfgSizeOfStruct = sizeof(XNetStartupParams);
331
332         // Bypass security so that we may connect to 'untrusted' hosts
333         xnsp.cfgFlags = XNET_STARTUP_BYPASS_SECURITY;
334   // create more memory for networking
335   xnsp.cfgPrivatePoolSizeInPages = 64; // == 256kb, default = 12 (48kb)
336   xnsp.cfgEnetReceiveQueueLength = 16; // == 32kb, default = 8 (16kb)
337   xnsp.cfgIpFragMaxSimultaneous = 16; // default = 4
338   xnsp.cfgIpFragMaxPacketDiv256 = 32; // == 8kb, default = 8 (2kb)
339   xnsp.cfgSockMaxSockets = 64; // default = 64
340   xnsp.cfgSockDefaultRecvBufsizeInK = 128; // default = 16
341   xnsp.cfgSockDefaultSendBufsizeInK = 128; // default = 16
342         INT err = XNetStartup(&xnsp);
343
344         XNADDR xna;
345         DWORD dwState;
346         do
347         {
348                 dwState = XNetGetTitleXnAddr(&xna);
349                 Sleep(500);
350         } while (dwState==XNET_GET_XNADDR_PENDING);
351
352         XNetInAddrToString(xna.ina,g_szTitleIP,32);
353
354         WSADATA WsaData;
355         err = WSAStartup( MAKEWORD(2,2), &WsaData );
356         return ( err == NO_ERROR );
357 }
358
359 static const __int64 SECS_BETWEEN_EPOCHS = 11644473600;
360 static const __int64 SECS_TO_100NS       = 10000000;
361
362 void CUtil::ConvertTimeTToFileTime(__int64 sec, long nsec, FILETIME &ftTime)
363 {
364         __int64 l64Result =((__int64)sec + SECS_BETWEEN_EPOCHS) + SECS_TO_100NS + (nsec / 100);
365         ftTime.dwLowDateTime = (DWORD)l64Result;
366         ftTime.dwHighDateTime = (DWORD)(l64Result>>32);
367 }
368
369
370 void CUtil::ReplaceExtension(const CStdString& strFile, const CStdString& strNewExtension, CStdString& strChangedFile)
371 {
372         CStdString strExtension;
373         GetExtension(strFile,strExtension);
374         if ( strExtension.size() )
375         {
376                 
377                 strChangedFile=strFile.substr(0, strFile.size()-strExtension.size()) ;
378                 strChangedFile+=strNewExtension;
379         }
380         else
381         {
382                 strChangedFile=strFile;
383                 strChangedFile+=strNewExtension;
384         }
385 }
386
387 void CUtil::GetExtension(const CStdString& strFile, CStdString& strExtension)
388 {
389         int iPos=strFile.ReverseFind(".");
390         if (iPos <0)
391         {
392                 strExtension="";
393                 return;
394         }
395         strExtension=strFile.Right( strFile.size()-iPos);
396 }
397
398 void CUtil::Lower(CStdString& strText)
399 {
400   char szText[1024];
401   strcpy(szText, strText.c_str());
402   for (int i=0; i < (int)strText.size();++i)
403     szText[i]=tolower(szText[i]);
404   strText=szText;
405 };
406
407 void CUtil::Unicode2Ansi(const wstring& wstrText,CStdString& strName)
408 {
409   strName="";
410   char *pstr=(char*)wstrText.c_str();
411   for (int i=0; i < (int)wstrText.size();++i )
412   {
413     strName += pstr[i*2];
414   }
415 }
416 bool CUtil::HasSlashAtEnd(const CStdString& strFile)
417 {
418   if (strFile.size()==0) return false;
419   char kar=strFile.c_str()[strFile.size()-1];
420   if (kar=='/' || kar=='\\') return true;
421   return false;
422 }
423
424  bool CUtil::IsRemote(const CStdString& strFile)
425 {
426         CURL url(strFile);
427         CStdString strProtocol=url.GetProtocol();
428         strProtocol.ToLower();
429         if (strProtocol=="cdda" || strProtocol=="iso9660") return false;
430         if ( url.GetProtocol().size() ) return true;
431         return false;
432 }
433
434  bool CUtil::IsDVD(const CStdString& strFile)
435 {
436         if (strFile.Left(2)=="D:" || strFile.Left(2)=="d:")
437                 return true;
438         return false;
439 }
440  bool CUtil::IsCDDA(const CStdString& strFile)
441 {
442         CStdString strLeft=strFile.Left(5);
443         strLeft.ToLower();
444         if (strLeft=="cdda:")
445                 return true;
446         return false;
447 }
448 bool CUtil::IsISO9660(const CStdString& strFile)
449 {
450         CStdString strLeft=strFile.Left(8);
451         strLeft.ToLower();
452         if (strLeft=="iso9660:")
453                 return true;
454         return false;
455 }
456
457 void CUtil::GetFileAndProtocol(const CStdString& strURL, CStdString& strDir)
458 {
459         strDir=strURL;
460         if (!IsRemote(strURL)) return;
461         if (IsDVD(strURL)) return;
462
463         CURL url(strURL);
464         strDir.Format("%s://%s", url.GetProtocol().c_str(), url.GetFileName().c_str());
465 }
466
467 void CUtil::RemoveCRLF(CStdString& strLine)
468 {
469         while ( strLine.size() && (strLine.Right(1)=="\n" || strLine.Right(1)=="\r") )
470         {
471                 strLine=strLine.Left((int)strLine.size()-1);
472         }
473
474 }
475 bool CUtil::IsPicture(const CStdString& strFile) 
476 {
477         CStdString strExtension;
478         CUtil::GetExtension(strFile,strExtension);
479   CUtil::Lower(strExtension);
480         if ( strstr( g_stSettings.m_szMyPicturesExtensions, strExtension.c_str() ) )
481         {
482                 return true;
483         }
484         return false;
485
486 }
487
488 bool CUtil::IsAudio(const CStdString& strFile) 
489 {
490         CStdString strExtension;
491         CUtil::GetExtension(strFile,strExtension);
492   CUtil::Lower(strExtension);
493         if ( strstr( g_stSettings.m_szMyMusicExtensions, strExtension.c_str() ) )
494         {
495                 return true;
496         }
497         return false;
498 }
499 bool CUtil::IsVideo(const CStdString& strFile) 
500 {
501         CStdString strExtension;
502         CUtil::GetExtension(strFile,strExtension);
503   CUtil::Lower(strExtension);
504         if ( strstr( g_stSettings.m_szMyVideoExtensions, strExtension.c_str() ) )
505         {
506                 return true;
507         }
508         return false;
509 }
510 bool CUtil::IsPlayList(const CStdString& strFile) 
511 {
512         CStdString strExtension;
513         CUtil::GetExtension(strFile,strExtension);
514         strExtension.ToLower();
515         if (strExtension==".m3u") return true;
516         if (strExtension==".b4s") return true;
517         if (strExtension==".pls") return true;
518         return false;
519 }
520  void CUtil::URLEncode(CStdString& strURLData)
521 {
522         CStdString strResult;
523         for (int i=0; i < (int)strURLData.size(); ++i)
524         {
525                         char kar=strURLData[i];
526                         if (kar==' ') strResult+='+';
527                         else if (isalnum(kar) || kar=='&'  || kar=='=' ) strResult+=kar;
528                         else {
529                                 CStdString strTmp;
530                                 strTmp.Format("%%%02.2x", kar);
531                                 strResult+=strTmp;
532                         }
533         }
534         strURLData=strResult;
535 }
536
537 void CUtil::SaveString(const CStdString &strTxt, FILE *fd)
538 {
539         int iSize=strTxt.size();
540         fwrite(&iSize,1,sizeof(int),fd);
541         if (iSize > 0)
542         {
543                 fwrite(&strTxt.c_str()[0],1,iSize,fd);
544         }
545 }
546
547 bool CUtil::LoadString(string &strTxt, FILE *fd)
548 {
549         strTxt="";
550         int iSize;
551         int iRead=fread(&iSize,1,sizeof(int),fd);
552         if (iRead != sizeof(int) ) return false;
553         if (feof(fd)) return false;
554         if (iSize==0) return true;
555         if (iSize > 0 && iSize < 16384)
556         {
557                 char *szTmp = new char [iSize+2];
558                 iRead=fread(szTmp,1,iSize,fd);
559                 if (iRead != iSize)
560                 {
561                         delete [] szTmp;
562                         return false;
563                 }
564                 szTmp[iSize]=0;
565                 strTxt=szTmp;
566                 delete [] szTmp;
567                 return true;
568         }
569         return false;
570 }
571
572 void CUtil::SaveInt(int iValue, FILE *fd)
573 {
574         fwrite(&iValue,1,sizeof(int),fd);
575 }
576
577 int CUtil::LoadInt( FILE *fd)
578 {
579         int iValue;
580         fread(&iValue,1,sizeof(int),fd);
581         return iValue;
582 }
583
584 void CUtil::LoadDateTime(SYSTEMTIME& dateTime, FILE *fd)
585 {
586         fread(&dateTime,1,sizeof(dateTime),fd);
587 }
588
589 void CUtil::SaveDateTime(SYSTEMTIME& dateTime, FILE *fd)
590 {
591         fwrite(&dateTime,1,sizeof(dateTime),fd);
592 }
593
594
595 void CUtil::GetSongInfo(const CStdString& strFileName, CStdString& strSongCacheName)
596 {
597         Crc32 crc;
598         crc.Reset();
599   crc.Compute(strFileName.c_str(),strlen(strFileName.c_str()));
600         strSongCacheName.Format("%s\\%x.si",g_stSettings.m_szAlbumDirectory,crc);
601 }
602
603 void CUtil::GetAlbumThumb(const CStdString& strFileName, CStdString& strThumb)
604 {
605         Crc32 crc;
606         crc.Reset();
607   crc.Compute(strFileName.c_str(),strlen(strFileName.c_str()));
608         strThumb.Format("%s\\albums\\%x.tbn",g_stSettings.m_szAlbumDirectory,crc);
609 }
610 void CUtil::GetAlbumInfo(const CStdString& strFileName, CStdString& strAlbumThumb)
611 {
612         CStdString strTmp="";
613         for (int i=0; i < (int)strFileName.size(); ++i)
614         {
615                 char kar=strFileName[i];
616                 if ( isalpha( (byte)kar) ) strTmp +=kar;
617         }
618         strTmp.ToLower();
619         Crc32 crc;
620         crc.Reset();
621   crc.Compute(strTmp.c_str(),strTmp.size());
622         strAlbumThumb.Format("%s\\albums\\%x.ai",g_stSettings.m_szAlbumDirectory,crc);
623 }
624
625 void CUtil::GetAlbumDatabase(const CStdString& strFileName, CStdString& strAlbumThumb)
626 {
627         CStdString strTmp="";
628         for (int i=0; i < (int)strFileName.size(); ++i)
629         {
630                 char kar=strFileName[i];
631                 if ( isalpha( (byte)kar) ) strTmp +=kar;
632         }
633         strTmp.ToLower();
634         Crc32 crc;
635         crc.Reset();
636   crc.Compute(strTmp.c_str(),strTmp.size());
637         strAlbumThumb.Format("%s\\albums\\%x.aldbs",g_stSettings.m_szAlbumDirectory,crc);
638 }
639
640 void CUtil::GetArtistDatabase(const CStdString& strFileName, CStdString& strArtistDBS)
641 {
642         CStdString strTmp="";
643         for (int i=0; i < (int)strFileName.size(); ++i)
644         {
645                 char kar=strFileName[i];
646                 if ( isalpha( (byte)kar) ) strTmp +=kar;
647         }
648         strTmp.ToLower();
649         Crc32 crc;
650         crc.Reset();
651   crc.Compute(strTmp.c_str(),strTmp.size());
652         strArtistDBS.Format("%s\\artists\\%x.ardbs",g_stSettings.m_szAlbumDirectory,crc);
653 }
654
655 void CUtil::GetGenreDatabase(const CStdString& strFileName, CStdString& strGenreDBS)
656 {
657         CStdString strTmp="";
658         for (int i=0; i < (int)strFileName.size(); ++i)
659         {
660                 char kar=strFileName[i];
661                 if ( isalpha( (byte)kar) ) strTmp +=kar;
662         }
663         strTmp.ToLower();
664         Crc32 crc;
665         crc.Reset();
666   crc.Compute(strTmp.c_str(),strTmp.size());
667         strGenreDBS.Format("%s\\genres\\%x.grdbs",g_stSettings.m_szAlbumDirectory,crc);
668 }
669 bool CUtil::GetXBEIcon(const CStdString& strFilePath, CStdString& strIcon)
670 {
671   // check if thumbnail already exists
672   char szThumbNail[1024];
673         Crc32 crc;
674         crc.Reset();
675   crc.Compute(strFilePath.c_str(),strlen(strFilePath.c_str()));
676   sprintf(szThumbNail,"%s\\%x.tbn",g_stSettings.szThumbnailsDirectory,crc);
677   strIcon= szThumbNail;
678   if (CUtil::FileExists(strIcon) )
679   {
680     //yes, just return
681     return true;
682   }
683
684   // no, then create a new thumb
685   // Locate file ID and get TitleImage.xbx E:\UDATA\<ID>\TitleImage.xbx
686
687   bool bFoundThumbnail=false;
688   CStdString szFileName;
689         szFileName.Format("E:\\UDATA\\%08x\\TitleImage.xbx", GetXbeID( strFilePath ) );
690                         
691   CXBPackedResource* pPackedResource = new CXBPackedResource();
692   if( SUCCEEDED( pPackedResource->Create( szFileName.c_str(), 1, NULL ) ) )
693   {
694     LPDIRECT3DTEXTURE8 pTexture;
695     LPDIRECT3DTEXTURE8 m_pTexture;
696                 D3DSURFACE_DESC descSurface;
697
698                 pTexture = pPackedResource->GetTexture((DWORD)0);
699
700                 if ( pTexture )
701                 {
702       if ( SUCCEEDED( pTexture->GetLevelDesc( 0, &descSurface ) ) )
703       {
704         int iHeight=descSurface.Height;
705         int iWidth=descSurface.Width;
706         DWORD dwFormat=descSurface.Format;
707         g_graphicsContext.Get3DDevice()->CreateTexture( 128,
708                                                                                         128,
709                                                                                         1,
710                                                                                         0,
711                                                                                         D3DFMT_LIN_A8R8G8B8,
712                                                                                         0,
713                                                                                         &m_pTexture);
714                                 LPDIRECT3DSURFACE8 pSrcSurface = NULL;
715                                 LPDIRECT3DSURFACE8 pDestSurface = NULL;
716
717         pTexture->GetSurfaceLevel( 0, &pSrcSurface );
718         m_pTexture->GetSurfaceLevel( 0, &pDestSurface );
719
720         D3DXLoadSurfaceFromSurface( pDestSurface, NULL, NULL, 
721                                     pSrcSurface, NULL, NULL,
722                                     D3DX_DEFAULT, D3DCOLOR( 0 ) );
723         D3DLOCKED_RECT rectLocked;
724         if ( D3D_OK == m_pTexture->LockRect(0,&rectLocked,NULL,0L  ) )
725         {
726                         BYTE *pBuff   = (BYTE*)rectLocked.pBits;        
727                         if (pBuff)
728                         {
729                                 DWORD strideScreen=rectLocked.Pitch;
730               //mp_msg(0,0," strideScreen=%i\n", strideScreen);
731
732               CSectionLoader::Load("CXIMAGE");
733               CxImage* pImage = new CxImage(iWidth, iHeight, 24, CXIMAGE_FORMAT_JPG);
734                                       for (int y=0; y < iHeight; y++)
735               {
736                 byte *pPtr = pBuff+(y*(strideScreen));
737                 for (int x=0; x < iWidth;x++)
738                 {
739                   byte Alpha=*(pPtr+3);
740                   byte b=*(pPtr+0);
741                   byte g=*(pPtr+1);
742                   byte r=*(pPtr+2);
743                   pPtr+=4;
744                   
745                   pImage->SetPixelColor(x,y,RGB(r,g,b));
746                 }
747               }
748
749               m_pTexture->UnlockRect(0);
750                           //mp_msg(0,0,"save as %s\n", szThumbNail);
751                           pImage->Resample(64,64,0);
752                           pImage->Flip();
753               pImage->Save(strIcon.c_str(),CXIMAGE_FORMAT_JPG);
754                           delete pImage;
755               bFoundThumbnail=true;
756               CSectionLoader::Unload("CXIMAGE");
757             }
758             else m_pTexture->UnlockRect(0);
759         }
760         pSrcSurface->Release();
761         pDestSurface->Release();
762         m_pTexture->Release();
763       }
764       pTexture->Release();
765     }
766   }
767   delete pPackedResource;
768   return bFoundThumbnail;
769 }
770
771
772 bool CUtil::GetXBEDescription(const CStdString& strFileName, CStdString& strDescription)
773 {
774
775                 _XBE_CERTIFICATE HC;
776                 _XBE_HEADER HS;
777
778                 FILE* hFile  = fopen(strFileName.c_str(),"rb");
779     if (!hFile)
780     {
781       strDescription=CUtil::GetFileName(strFileName);
782       return false;
783     }
784                 fread(&HS,1,sizeof(HS),hFile);
785                 fseek(hFile,HS.XbeHeaderSize,SEEK_SET);
786                 fread(&HC,1,sizeof(HC),hFile);
787                 fclose(hFile);
788
789                 CHAR TitleName[40];
790                 WideCharToMultiByte(CP_ACP,0,HC.TitleName,-1,TitleName,40,NULL,NULL);
791     if (strlen(TitleName) > 0)
792     {
793                   strDescription=TitleName;
794       return true;
795     }
796     strDescription=CUtil::GetFileName(strFileName);
797     return false;
798 }
799
800 DWORD CUtil::GetXbeID( const CStdString& strFilePath)
801 {
802         DWORD dwReturn = 0;
803         HANDLE hFile;
804         DWORD dwCertificateLocation;
805         DWORD dwLoadAddress;
806         DWORD dwRead;
807 //      WCHAR wcTitle[41];
808         
809   hFile = CreateFile( strFilePath.c_str(), 
810                                                 GENERIC_READ, 
811                                                 FILE_SHARE_READ, 
812                                                 NULL,
813                                                 OPEN_EXISTING,
814                                                 FILE_ATTRIBUTE_NORMAL,
815                                                 NULL );
816         if ( hFile != INVALID_HANDLE_VALUE )
817         {
818                 if ( SetFilePointer(    hFile,  0x104, NULL, FILE_BEGIN ) == 0x104 )
819                 {
820                         if ( ReadFile( hFile, &dwLoadAddress, 4, &dwRead, NULL ) )
821                         {
822                                 if ( SetFilePointer(    hFile,  0x118, NULL, FILE_BEGIN ) == 0x118 )
823                                 {
824                                         if ( ReadFile( hFile, &dwCertificateLocation, 4, &dwRead, NULL ) )
825                                         {
826                                                 dwCertificateLocation -= dwLoadAddress;
827                                                 // Add offset into file
828                                                 dwCertificateLocation += 8;
829                                                 if ( SetFilePointer(    hFile,  dwCertificateLocation, NULL, FILE_BEGIN ) == dwCertificateLocation )
830                                                 {
831                                                         dwReturn = 0;
832                                                         ReadFile( hFile, &dwReturn, sizeof(DWORD), &dwRead, NULL );
833                                                         if ( dwRead != sizeof(DWORD) )
834                                                         {
835                                                                 dwReturn = 0;
836                                                         }
837                                                 }
838
839                                         }
840                                 }
841                         }
842                 }
843                 CloseHandle(hFile);
844         }
845         return dwReturn;
846 }
847 void CUtil::FillInDefaultIcons(VECFILEITEMS &items)
848 {
849         for (int i=0; i < (int)items.size(); ++i)
850         {
851                 CFileItem* pItem=items[i];
852         
853                 if (pItem->m_bIsFolder)
854                 {
855                         pItem->SetIconImage("defaultFolder.png");
856                 }
857
858                 if (pItem->GetIconImage()=="")
859                 {
860                         if (!pItem->m_bIsFolder)
861                         {
862                                 CStdString strExtension;
863                                 CUtil::GetExtension(pItem->m_strPath,strExtension);
864                                 
865                                 for (int i=0; i < (int)g_settings.m_vecIcons.size(); ++i)
866                                 {
867                                         CFileTypeIcon& icon=g_settings.m_vecIcons[i];
868
869                                         if (CUtil::cmpnocase(strExtension.c_str(), icon.m_strName)==0)
870                                         {
871                                                 pItem->SetIconImage(icon.m_strIcon);
872                                                 break;
873                                         }
874                                 }
875                         }
876                 }
877
878                 if (pItem->GetThumbnailImage()=="")
879                 {
880                         if (pItem->GetIconImage()!="")
881                         {
882                                 CStdString strBig;
883                                 int iPos=pItem->GetIconImage().Find(".");
884                                 strBig=pItem->GetIconImage().Left(iPos);
885                                 strBig+="Big";
886                                 strBig+=pItem->GetIconImage().Right(pItem->GetIconImage().size()-(iPos));
887                                 pItem->SetThumbnailImage(strBig);
888                         }
889                 }
890         }
891 }
892
893 void CUtil::SetThumbs(VECFILEITEMS &items)
894 {
895   CStdString strThumb;
896   for (int i=0; i < (int)items.size(); ++i)
897   {
898     CFileItem* pItem=items[i];
899                 pItem->m_bIsShareOrDrive=false;
900                 if (!pItem->m_bIsFolder)
901                 {
902                         // picture
903                         if (CUtil::IsPicture(pItem->m_strPath) )
904                         {
905                                 pItem->SetIconImage("defaultPicture.png");
906                         }
907
908                         // album database
909                         if (CUtil::IsAudio(pItem->m_strPath) ||
910                                         pItem->m_strPath.Find(".aldbs")>=0)
911                         {
912                                 pItem->SetIconImage("defaultAudio.png");
913                         }
914
915                         // xbe
916                         if (CUtil::IsXBE(pItem->m_strPath) )
917                         {
918                                 pItem->SetIconImage("defaultProgram.png");
919                                 if ( !CUtil::IsDVD(pItem->m_strPath) )
920         {
921                                         CStdString strDescription;
922                                         if (! CUtil::GetXBEDescription(pItem->m_strPath,strDescription))
923           {
924                                                 CStdString strFName=CUtil::GetFileName(pItem->m_strPath);
925                                                 strDescription=pItem->m_strPath.Left(pItem->m_strPath.size()-strFName.size());
926                                                 if (CUtil::HasSlashAtEnd(strDescription) )
927                                                 {
928                                                         strDescription=strDescription.Left(strDescription.size()-1);
929                                                 }
930                                                 int iPos=strDescription.ReverseFind("\\");
931                                                 if (iPos < 0)
932                                                         iPos=strDescription.ReverseFind("/");
933                                                 if (iPos >=0)
934                                                 {
935                                                         strDescription=strDescription.Right(strDescription.size()-iPos);
936                                                 }
937                                                 else strDescription=strFName;
938                                         }
939                                         if (strDescription.size())
940                                         {
941                                                 CShortcut cut;
942                                                 cut.m_strPath=pItem->m_strPath;
943                                                 cut.Save(strDescription);
944                                         }
945         }
946                         }
947
948                         // video
949                         if (CUtil::IsVideo(pItem->m_strPath) )
950                         {
951                                 pItem->SetIconImage("defaultVideo.png");
952                         }
953
954                         // playlist
955                         if (CUtil::IsPlayList(pItem->m_strPath) )
956                         {
957                                 CStdString strDir;
958                                 CStdString strFileName;
959                                 pItem->SetIconImage("defaultPlaylist.png");
960                                 strFileName=CUtil::GetFileName(pItem->m_strPath);
961                                 strDir.Format("%s\\playlists\\%s",g_stSettings.m_szAlbumDirectory,strFileName.c_str());
962                                 if ( strDir != pItem->m_strPath )
963                                 {
964                                         CFile file;
965                                         file.Cache(pItem->m_strPath, strDir);
966                                 }
967                         }
968                         if (CUtil::IsShortCut(pItem->m_strPath) )
969                         {
970                                 pItem->SetIconImage("defaultShortcut.png");
971                         }
972                 }
973                 else
974                 {
975                         if (pItem->GetLabel()=="..")
976                         {
977                                 pItem->SetIconImage("defaultFolderBack.png");
978                         }
979                 }
980                 
981                 if ( !pItem->HasThumbnail() )
982                 {
983                         CUtil::GetThumbnail( pItem->m_strPath,strThumb);
984                         if (!CUtil::FileExists(strThumb) )
985                         {
986                                 if (CUtil::IsRemote(strThumb) )
987                                 {
988                                         CFile file;
989                                         CStdString strThumbnailFileName;
990                                         CUtil::ReplaceExtension(pItem->m_strPath,".tbn", strThumbnailFileName);
991                                         if ( file.Cache(strThumbnailFileName.c_str(), strThumb.c_str()))
992                                         {
993                                                 pItem->SetThumbnailImage(strThumb);
994                                         }
995                                 }
996                         }
997                         else
998                         {
999                                         pItem->SetThumbnailImage(strThumb);
1000                         }
1001                 }
1002   }
1003 }
1004
1005 void CUtil::ShortenFileName(CStdString& strFileNameAndPath)
1006 {
1007         CStdString strFile=CUtil::GetFileName(strFileNameAndPath);
1008         if (strFile.size() > 42)
1009         {
1010                 CStdString strExtension;
1011                 CUtil::GetExtension(strFileNameAndPath, strExtension);
1012                 CStdString strPath=strFileNameAndPath.Left( strFileNameAndPath.size() - strFile.size() );
1013                 
1014                 strFile=strFile.Left(42-strExtension.size());
1015                 strFile+=strExtension;
1016
1017                 CStdString strNewFile=strPath;
1018                 if (!CUtil::HasSlashAtEnd(strPath)) 
1019                         strNewFile+="\\";
1020
1021                 strNewFile+=strFile;
1022                 strFileNameAndPath=strNewFile;
1023         }
1024 }
1025
1026 void CUtil::GetIMDBInfo(const CStdString& strFileName, CStdString& strImbInfo)
1027 {
1028         CStdString strTmp="";
1029         for (int i=0; i < (int)strFileName.size(); ++i)
1030         {
1031                 char kar=strFileName[i];
1032                 if ( isalpha( (byte)kar) ) strTmp +=kar;
1033         }
1034         strTmp.ToLower();
1035         Crc32 crc;
1036         crc.Reset();
1037   crc.Compute(strTmp.c_str(),strTmp.size());
1038         strImbInfo.Format("%s\\%x.imdb",g_stSettings.m_szIMDBDirectory,crc);
1039 }