[cstdstring] demise Format, replacing with StringUtils::Format
[vuplus_xbmc] / xbmc / interfaces / legacy / ModuleXbmc.cpp
1 /*
2  *      Copyright (C) 2005-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 // TODO: Need a uniform way of returning an error status
22
23 #if (defined HAVE_CONFIG_H) && (!defined TARGET_WINDOWS)
24   #include "config.h"
25 #endif
26 #include "network/Network.h"
27
28 #include "ModuleXbmc.h"
29
30 #include "Application.h"
31 #include "ApplicationMessenger.h"
32 #include "utils/URIUtils.h"
33 #include "aojsonrpc.h"
34 #ifndef TARGET_WINDOWS
35 #include "XTimeUtils.h"
36 #endif
37 #include "guilib/LocalizeStrings.h"
38 #include "GUIInfoManager.h"
39 #include "guilib/GUIAudioManager.h"
40 #include "guilib/GUIWindowManager.h"
41 #include "filesystem/File.h"
42 #include "filesystem/SpecialProtocol.h"
43 #include "utils/Crc32.h"
44 #include "FileItem.h"
45 #include "LangInfo.h"
46 #include "settings/AdvancedSettings.h"
47 #include "settings/Settings.h"
48 #include "guilib/TextureManager.h"
49 #include "Util.h"
50 #include "URL.h"
51 #include "cores/AudioEngine/AEFactory.h"
52 #include "storage/MediaManager.h"
53 #include "utils/FileUtils.h"
54 #include "utils/LangCodeExpander.h"
55 #include "utils/StringUtils.h"
56 #include "CallbackHandler.h"
57 #include "AddonUtils.h"
58
59 #include "LanguageHook.h"
60
61 #include "cores/VideoRenderers/RenderCapture.h"
62
63 #include "threads/SystemClock.h"
64 #include "Exception.h"
65 #include <vector>
66
67 namespace XBMCAddon
68 {
69
70   namespace xbmc
71   {
72     /*****************************************************************
73      * start of xbmc methods
74      *****************************************************************/
75     void log(const char* msg, int level)
76     {
77       // check for a valid loglevel
78       if (level < LOGDEBUG || level > LOGNONE)
79         level = LOGNOTICE;
80       CLog::Log(level, "%s", msg);
81     }
82
83     void shutdown()
84     {
85       XBMC_TRACE;
86       ThreadMessage tMsg = {TMSG_SHUTDOWN};
87       CApplicationMessenger::Get().SendMessage(tMsg);
88     }
89
90     void restart()
91     {
92       XBMC_TRACE;
93       ThreadMessage tMsg = {TMSG_RESTART};
94       CApplicationMessenger::Get().SendMessage(tMsg);
95     }
96
97     void executescript(const char* script)
98     {
99       XBMC_TRACE;
100       if (! script)
101         return;
102
103       ThreadMessage tMsg = {TMSG_EXECUTE_SCRIPT};
104       tMsg.strParam = script;
105       CApplicationMessenger::Get().SendMessage(tMsg);
106     }
107
108     void executebuiltin(const char* function, bool wait /* = false*/)
109     {
110       XBMC_TRACE;
111       if (! function)
112         return;
113       CApplicationMessenger::Get().ExecBuiltIn(function,wait);
114     }
115
116     String executehttpapi(const char* httpcommand) 
117     {
118       XBMC_TRACE;
119       THROW_UNIMP("executehttpapi");
120     }
121
122     String executeJSONRPC(const char* jsonrpccommand)
123     {
124       XBMC_TRACE;
125       DelayedCallGuard dg;
126 #ifdef HAS_JSONRPC
127       String ret;
128
129       if (! jsonrpccommand)
130         return ret;
131
132       //    String method = jsonrpccommand;
133
134       CAddOnTransport transport;
135       CAddOnTransport::CAddOnClient client;
136
137       return JSONRPC::CJSONRPC::MethodCall(/*method*/ jsonrpccommand, &transport, &client);
138 #else
139       THROW_UNIMP("executeJSONRPC");
140 #endif
141     }
142
143     void sleep(long timemillis)
144     {
145       XBMC_TRACE;
146
147       XbmcThreads::EndTime endTime(timemillis);
148       while (!endTime.IsTimePast())
149       {
150         LanguageHook* lh = NULL;
151         {
152           DelayedCallGuard dcguard;
153           lh = dcguard.getLanguageHook(); // borrow this
154           long nextSleep = endTime.MillisLeft();
155           if (nextSleep > 100)
156             nextSleep = 100; // only sleep for 100 millis
157           ::Sleep(nextSleep);
158         }
159         if (lh != NULL)
160           lh->MakePendingCalls();
161       }
162     }
163
164     String getLocalizedString(int id)
165     {
166       XBMC_TRACE;
167       String label;
168       if (id >= 30000 && id <= 30999)
169         label = g_localizeStringsTemp.Get(id);
170       else if (id >= 32000 && id <= 32999)
171         label = g_localizeStringsTemp.Get(id);
172       else
173         label = g_localizeStrings.Get(id);
174
175       return label;
176     }
177
178     String getSkinDir()
179     {
180       XBMC_TRACE;
181       return CSettings::Get().GetString("lookandfeel.skin");
182     }
183
184     String getLanguage(int format /* = CLangCodeExpander::ENGLISH_NAME */, bool region /*= false*/)
185     {
186       XBMC_TRACE;
187       CStdString lang = CSettings::Get().GetString("locale.language");
188
189       switch (format)
190       {
191       case CLangCodeExpander::ENGLISH_NAME:
192         {
193           if (region)
194           {
195             CStdString region = "-" + g_langInfo.GetCurrentRegion();
196             return (lang += region);
197           }
198           return lang;
199         }
200       case CLangCodeExpander::ISO_639_1:
201         {
202           CStdString langCode;
203           g_LangCodeExpander.ConvertToTwoCharCode(langCode, lang);
204           if (region)
205           {
206             CStdString region = g_langInfo.GetRegionLocale();
207             CStdString region2Code;
208             g_LangCodeExpander.ConvertToTwoCharCode(region2Code, region);
209             region2Code = "-" + region2Code;
210             return (langCode += region2Code);
211           }
212           return langCode;
213         }
214       case CLangCodeExpander::ISO_639_2:
215         {
216           CStdString langCode;
217           g_LangCodeExpander.ConvertToThreeCharCode(langCode, lang);
218           if (region)
219           {
220             CStdString region = g_langInfo.GetRegionLocale();
221             CStdString region3Code;
222             g_LangCodeExpander.ConvertToThreeCharCode(region3Code, region);
223             region3Code = "-" + region3Code;
224             return (langCode += region3Code);
225           }
226
227           return langCode;
228         }
229       default:
230         return "";
231       }
232     }
233
234     String getIPAddress()
235     {
236       XBMC_TRACE;
237       char cTitleIP[32];
238       sprintf(cTitleIP, "127.0.0.1");
239       CNetworkInterface* iface = g_application.getNetwork().GetFirstConnectedInterface();
240       if (iface)
241         return iface->GetCurrentIPAddress();
242
243       return cTitleIP;
244     }
245
246     long getDVDState()
247     {
248       XBMC_TRACE;
249       return g_mediaManager.GetDriveStatus();
250     }
251
252     long getFreeMem()
253     {
254       XBMC_TRACE;
255       MEMORYSTATUSEX stat;
256       stat.dwLength = sizeof(MEMORYSTATUSEX);
257       GlobalMemoryStatusEx(&stat);
258       return (long)(stat.ullAvailPhys  / ( 1024 * 1024 ));
259     }
260
261     // getCpuTemp() method
262     // ## Doesn't work right, use getInfoLabel('System.CPUTemperature') instead.
263     /*PyDoc_STRVAR(getCpuTemp__doc__,
264       "getCpuTemp() -- Returns the current cpu temperature as an integer."
265       ""
266       "example:"
267       "  - cputemp = xbmc.getCpuTemp()");
268
269       PyObject* XBMC_GetCpuTemp(PyObject *self, PyObject *args)
270       {
271       unsigned short cputemp;
272       unsigned short cpudec;
273
274       _outp(0xc004, (0x4c<<1)|0x01);
275       _outp(0xc008, 0x01);
276       _outpw(0xc000, _inpw(0xc000));
277       _outp(0xc002, (0) ? 0x0b : 0x0a);
278       while ((_inp(0xc000) & 8));
279       cputemp = _inpw(0xc006);
280
281       _outp(0xc004, (0x4c<<1)|0x01);
282       _outp(0xc008, 0x10);
283       _outpw(0xc000, _inpw(0xc000));
284       _outp(0xc002, (0) ? 0x0b : 0x0a);
285       while ((_inp(0xc000) & 8));
286       cpudec = _inpw(0xc006);
287
288       if (cpudec<10) cpudec = cpudec * 100;
289       if (cpudec<100) cpudec = cpudec *10;
290
291       return PyInt_FromLong((long)(cputemp + cpudec / 1000.0f));
292       }*/
293
294     String getInfoLabel(const char* cLine)
295     {
296       XBMC_TRACE;
297       if (!cLine)
298       {
299         String ret;
300         return ret;
301       }
302
303       int ret = g_infoManager.TranslateString(cLine);
304       //doesn't seem to be a single InfoTag?
305       //try full blown GuiInfoLabel then
306       if (ret == 0)
307       {
308         CGUIInfoLabel label(cLine);
309         return label.GetLabel(0);
310       }
311       else
312         return g_infoManager.GetLabel(ret);
313     }
314
315     String getInfoImage(const char * infotag)
316     {
317       XBMC_TRACE;
318       if (!infotag)
319         {
320           String ret;
321           return ret;
322         }
323
324       int ret = g_infoManager.TranslateString(infotag);
325       return g_infoManager.GetImage(ret, WINDOW_INVALID);
326     }
327
328     void playSFX(const char* filename)
329     {
330       XBMC_TRACE;
331       if (!filename)
332         return;
333
334       if (XFILE::CFile::Exists(filename))
335       {
336         g_audioManager.PlayPythonSound(filename);
337       }
338     }
339
340     void enableNavSounds(bool yesNo)
341     {
342       XBMC_TRACE;
343       g_audioManager.Enable(yesNo);
344     }
345
346     bool getCondVisibility(const char *condition)
347     {
348       XBMC_TRACE;
349       if (!condition)
350         return false;
351
352       bool ret;
353       {
354         LOCKGUI;
355
356         int id = g_windowManager.GetTopMostModalDialogID();
357         if (id == WINDOW_INVALID) id = g_windowManager.GetActiveWindow();
358         ret = g_infoManager.EvaluateBool(condition,id);
359       }
360
361       return ret;
362     }
363
364     int getGlobalIdleTime()
365     {
366       XBMC_TRACE;
367       return g_application.GlobalIdleTime();
368     }
369
370     String getCacheThumbName(const String& path)
371     {
372       XBMC_TRACE;
373       Crc32 crc;
374       crc.ComputeFromLowerCase(path);
375       return StringUtils::Format("%08x.tbn", (unsigned __int32)crc);;
376     }
377
378     String makeLegalFilename(const String& filename, bool fatX)
379     {
380       XBMC_TRACE;
381       return CUtil::MakeLegalPath(filename);
382     }
383
384     String translatePath(const String& path)
385     {
386       XBMC_TRACE;
387       return CSpecialProtocol::TranslatePath(path);
388     }
389
390     Tuple<String,String> getCleanMovieTitle(const String& path, bool usefoldername)
391     {
392       XBMC_TRACE;
393       CFileItem item(path, false);
394       CStdString strName = item.GetMovieName(usefoldername);
395
396       CStdString strTitleAndYear;
397       CStdString strTitle;
398       CStdString strYear;
399       CUtil::CleanString(strName, strTitle, strTitleAndYear, strYear, usefoldername);
400       return Tuple<String,String>(strTitle,strYear);
401     }
402
403     String validatePath(const String& path)
404     {
405       XBMC_TRACE;
406       return CUtil::ValidatePath(path, true);
407     }
408
409     String getRegion(const char* id)
410     {
411       XBMC_TRACE;
412       CStdString result;
413
414       if (strcmpi(id, "datelong") == 0)
415         {
416           result = g_langInfo.GetDateFormat(true);
417           result.Replace("DDDD", "%A");
418           result.Replace("MMMM", "%B");
419           result.Replace("D", "%d");
420           result.Replace("YYYY", "%Y");
421         }
422       else if (strcmpi(id, "dateshort") == 0)
423         {
424           result = g_langInfo.GetDateFormat(false);
425           result.Replace("MM", "%m");
426           result.Replace("DD", "%d");
427           result.Replace("YYYY", "%Y");
428         }
429       else if (strcmpi(id, "tempunit") == 0)
430         result = g_langInfo.GetTempUnitString();
431       else if (strcmpi(id, "speedunit") == 0)
432         result = g_langInfo.GetSpeedUnitString();
433       else if (strcmpi(id, "time") == 0)
434         {
435           result = g_langInfo.GetTimeFormat();
436           result.Replace("H", "%H");
437           result.Replace("h", "%I");
438           result.Replace("mm", "%M");
439           result.Replace("ss", "%S");
440           result.Replace("xx", "%p");
441         }
442       else if (strcmpi(id, "meridiem") == 0)
443         result = StringUtils::Format("%s/%s",
444                                      g_langInfo.GetMeridiemSymbol(CLangInfo::MERIDIEM_SYMBOL_AM).c_str(),
445                                      g_langInfo.GetMeridiemSymbol(CLangInfo::MERIDIEM_SYMBOL_PM).c_str());
446
447       return result;
448     }
449
450     // TODO: Add a mediaType enum
451     String getSupportedMedia(const char* mediaType)
452     {
453       XBMC_TRACE;
454       String result;
455       if (strcmpi(mediaType, "video") == 0)
456         result = g_advancedSettings.m_videoExtensions;
457       else if (strcmpi(mediaType, "music") == 0)
458         result = g_advancedSettings.m_musicExtensions;
459       else if (strcmpi(mediaType, "picture") == 0)
460         result = g_advancedSettings.m_pictureExtensions;
461
462       // TODO:
463       //    else
464       //      return an error
465
466       return result;
467     }
468
469     bool skinHasImage(const char* image)
470     {
471       XBMC_TRACE;
472       return g_TextureManager.HasTexture(image);
473     }
474
475
476     bool startServer(int iTyp, bool bStart, bool bWait)
477     {
478       XBMC_TRACE;
479       DelayedCallGuard dg;
480       return g_application.StartServer((CApplication::ESERVERS)iTyp, bStart != 0, bWait != 0);
481     }
482
483     void audioSuspend()
484     {  
485       CAEFactory::Suspend();
486     }
487
488     void audioResume()
489     { 
490       CAEFactory::Resume();
491     }
492
493     String convertLanguage(const char* language, int format)
494     {
495       CStdString convertedLanguage;
496       switch (format)
497       {
498       case CLangCodeExpander::ENGLISH_NAME:
499         {
500           g_LangCodeExpander.Lookup(convertedLanguage, language);
501           // maybe it's a check whether the language exists or not
502           if (convertedLanguage.empty())
503           {
504             g_LangCodeExpander.ConvertToThreeCharCode(convertedLanguage, language);
505             g_LangCodeExpander.Lookup(convertedLanguage, convertedLanguage);
506           }
507           break;
508         }
509       case CLangCodeExpander::ISO_639_1:
510         g_LangCodeExpander.ConvertToTwoCharCode(convertedLanguage, language);
511         break;
512       case CLangCodeExpander::ISO_639_2:
513         g_LangCodeExpander.ConvertToThreeCharCode(convertedLanguage, language);
514         break;
515       default:
516         return "";
517       }
518       return convertedLanguage;
519     }
520
521     int getSERVER_WEBSERVER() { return CApplication::ES_WEBSERVER; }
522     int getSERVER_AIRPLAYSERVER() { return CApplication::ES_AIRPLAYSERVER; }
523     int getSERVER_UPNPSERVER() { return CApplication::ES_UPNPSERVER; }
524     int getSERVER_UPNPRENDERER() { return CApplication::ES_UPNPRENDERER; }
525     int getSERVER_EVENTSERVER() { return CApplication::ES_EVENTSERVER; }
526     int getSERVER_JSONRPCSERVER() { return CApplication::ES_JSONRPCSERVER; }
527     int getSERVER_ZEROCONF() { return CApplication::ES_ZEROCONF; }
528
529     int getPLAYLIST_MUSIC() { return PLAYLIST_MUSIC; }
530     int getPLAYLIST_VIDEO() { return PLAYLIST_VIDEO; }
531     int getPLAYER_CORE_AUTO() { return EPC_NONE; }
532     int getPLAYER_CORE_DVDPLAYER() { return EPC_DVDPLAYER; }
533     int getPLAYER_CORE_MPLAYER() { return EPC_MPLAYER; }
534     int getPLAYER_CORE_PAPLAYER() { return EPC_PAPLAYER; }
535     int getTRAY_OPEN() { return TRAY_OPEN; }
536     int getDRIVE_NOT_READY() { return DRIVE_NOT_READY; }
537     int getTRAY_CLOSED_NO_MEDIA() { return TRAY_CLOSED_NO_MEDIA; }
538     int getTRAY_CLOSED_MEDIA_PRESENT() { return TRAY_CLOSED_MEDIA_PRESENT; }
539     int getLOGDEBUG() { return LOGDEBUG; }
540     int getLOGINFO() { return LOGINFO; }
541     int getLOGNOTICE() { return LOGNOTICE; }
542     int getLOGWARNING() { return LOGWARNING; }
543     int getLOGERROR() { return LOGERROR; }
544     int getLOGSEVERE() { return LOGSEVERE; }
545     int getLOGFATAL() { return LOGFATAL; }
546     int getLOGNONE() { return LOGNONE; }
547
548     // render capture user states
549     int getCAPTURE_STATE_WORKING() { return CAPTURESTATE_WORKING; }
550     int getCAPTURE_STATE_DONE(){ return CAPTURESTATE_DONE; }
551     int getCAPTURE_STATE_FAILED() { return CAPTURESTATE_FAILED; }
552
553     // render capture flags
554     int getCAPTURE_FLAG_CONTINUOUS() { return (int)CAPTUREFLAG_CONTINUOUS; }
555     int getCAPTURE_FLAG_IMMEDIATELY() { return (int)CAPTUREFLAG_IMMEDIATELY; }
556
557     // language string formats
558     int getISO_639_1() { return CLangCodeExpander::ISO_639_1; } 
559     int getISO_639_2(){ return CLangCodeExpander::ISO_639_2; }
560     int getENGLISH_NAME() { return CLangCodeExpander::ENGLISH_NAME; }
561
562     const int lLOGNOTICE = LOGNOTICE;
563   }
564 }