]> Shamusworld >> Repos - architektonas/blob - src/base/rs_system.cpp
084fbbc26ed14390512b8e2d0f5985ca6643bd16
[architektonas] / src / base / rs_system.cpp
1 // rs_system.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  05/21/2010  Added this text. :-)
13 //
14
15 #include "rs_system.h"
16
17 #include <QTranslator>
18 #include "settings.h"
19
20 RS_System * RS_System::uniqueInstance = NULL;
21
22 // Constructor
23 RS_System::RS_System()
24 {
25         initialized = false;
26 }
27
28 /**
29  * @return Instance to the unique system object.
30  */
31 /*static*/ RS_System * RS_System::instance()
32 {
33         if (uniqueInstance == NULL)
34                 uniqueInstance = new RS_System();
35
36         return uniqueInstance;
37 }
38
39 /**
40  * Initializes the system.
41  *
42  * @param appName Application name (e.g. "QCad II")
43  * @param appVersion Application version (e.g. "1.2.3")
44  * @param appDirName Application directory name used for
45  *     subdirectories in /usr, /etc ~/.
46  * @param appDir Absolute application directory (e.g. /opt/qcad)
47  *                 defaults to current directory.
48  */
49 void RS_System::init(const QString & appName, const QString & appVersion,
50         const QString & appDirName, const QString & appDir)
51 {
52         this->appName = appName;
53         this->appVersion = appVersion;
54         this->appDirName = appDirName;
55
56         if (appDir == "")
57         {
58 //        this->appDir = QDir::currentDirPath();
59                 this->appDir = QDir::currentPath();
60         }
61         else
62         {
63                 this->appDir = appDir;
64         }
65
66         RS_DEBUG->print("RS_System::init: System %s initialized.", appName.toLatin1().data());
67         RS_DEBUG->print("RS_System::init: App dir: %s", appDir.toLatin1().data());
68         initialized = true;
69
70         initLanguageList();
71 }
72
73 /**
74  * Initializes the list of available translations.
75  */
76 void RS_System::initLanguageList()
77 {
78         RS_DEBUG->print("RS_System::initLanguageList");
79         QStringList lst = getFileList("qm", "qm");
80
81         settings.beginGroup("Paths");
82 //      lst += QStringList::split(";", RS_SETTINGS->readEntry("/Translations", ""));
83         lst += settings.value("Translations", "").toString().split(";");
84         settings.endGroup();
85
86         for(QStringList::Iterator it=lst.begin(); it!=lst.end(); ++it)
87         {
88                 RS_DEBUG->print("RS_System::initLanguageList: qm file: %s", (*it).toLatin1().data());
89
90 //              int i1 = (*it).findRev('_');
91                 int i1 = (*it).lastIndexOf('_');
92 //              int i2 = (*it).find('.', i1);
93                 int i2 = (*it).indexOf('.', i1);
94                 QString l = (*it).mid(i1 + 1, i2 - i1 - 1);
95
96 //              if (languageList.find(l) == languageList.end())
97                 if (languageList.indexOf(l) == -1)
98                 {
99                         RS_DEBUG->print("RS_System::initLanguageList: append language: %s", l.toLatin1().data());
100                         languageList.append(l);
101                 }
102         }
103
104         RS_DEBUG->print("RS_System::initLanguageList: OK");
105 }
106
107 /**
108  * Loads a different translation for the application GUI.
109  */
110 void RS_System::loadTranslation(const QString & lang, const QString & langCmd)
111 {
112         static QTranslator * tQt = NULL;
113         static QTranslator * tQCad = NULL;
114         static QTranslator * tQCadGuiQt = NULL;
115         static QTranslator * tQCadActions = NULL;
116         static QTranslator * tQCadCmd = NULL;
117         static QTranslator * tQCadLib = NULL;
118         static QTranslator * tQCadCam = NULL;
119         static QTranslator * tQCadProf = NULL;
120
121         QString langFile;
122
123         // search in various directories for translations
124         QStringList lst = getDirectoryList("qm");
125
126         settings.beginGroup("Paths");
127 //      lst += QStringList::split(";", RS_SETTINGS->readEntry("/Translations", ""));
128         lst += settings.value("Translations", "").toString().split(";");
129         settings.endGroup();
130
131         for(QStringList::Iterator it=lst.begin(); it!=lst.end(); ++it)
132         {
133                 langFile = "qt_" + lang + ".qm";
134
135                 if (tQt != NULL)
136                 {
137                         qApp->removeTranslator(tQt);
138                         delete tQt;
139                 }
140
141                 tQt = new QTranslator(0);
142
143                 if (tQt->load(langFile, (*it)))
144                 {
145                         qApp->installTranslator(tQt);
146                 }
147
148                 langFile = "qcad_" + lang + ".qm";
149
150                 if (tQCad != NULL)
151                 {
152                         qApp->removeTranslator(tQCad);
153                         delete tQCad;
154                 }
155
156                 tQCad = new QTranslator(0);
157
158                 if (tQCad->load(langFile, (*it)))
159                 {
160                         qApp->installTranslator(tQCad);
161                 }
162
163                 langFile = "qcadguiqt_" + lang + ".qm";
164
165                 if (tQCadGuiQt != NULL)
166                 {
167                         qApp->removeTranslator(tQCadGuiQt);
168                         delete tQCadGuiQt;
169                 }
170
171                 tQCadGuiQt = new QTranslator(0);
172
173                 if (tQCadGuiQt->load(langFile, (*it)))
174                 {
175                         qApp->installTranslator(tQCadGuiQt);
176                 }
177
178                 langFile = "qcadactions_" + lang + ".qm";
179
180                 if (tQCadActions != NULL)
181                 {
182                         qApp->removeTranslator(tQCadActions);
183                         delete tQCadActions;
184                 }
185
186                 tQCadActions = new QTranslator(0);
187
188                 if (tQCadActions->load(langFile, (*it)))
189                 {
190                         qApp->installTranslator(tQCadActions);
191                 }
192
193                 langFile = "qcadcmd_" + langCmd + ".qm";
194
195                 if (tQCadCmd != NULL)
196                 {
197                         qApp->removeTranslator(tQCadCmd);
198                         delete tQCadCmd;
199                 }
200
201                 tQCadCmd = new QTranslator(0);
202
203                 if (tQCadCmd->load(langFile, (*it)))
204                 {
205                         qApp->installTranslator(tQCadCmd);
206                 }
207
208                 langFile = "qcadlib_" + lang + ".qm";
209
210                 if (tQCadLib != NULL)
211                 {
212                         qApp->removeTranslator(tQCadLib);
213                         delete tQCadLib;
214                 }
215
216                 tQCadLib = new QTranslator(0);
217
218                 if (tQCadLib->load(langFile, (*it)))
219                 {
220                         qApp->installTranslator(tQCadLib);
221                 }
222
223                 langFile = "qcadcam_" + lang + ".qm";
224
225                 if (tQCadLib != NULL)
226                 {
227                         qApp->removeTranslator(tQCadCam);
228                         delete tQCadCam;
229                 }
230
231                 tQCadCam = new QTranslator(0);
232
233                 if (tQCadCam->load(langFile, (*it)))
234                 {
235                         qApp->installTranslator(tQCadCam);
236                 }
237
238                 langFile = "qcadprof_" + lang + ".qm";
239
240                 if (tQCadProf != NULL)
241                 {
242                         qApp->removeTranslator(tQCadProf);
243                         delete tQCadProf;
244                 }
245
246                 tQCadProf = new QTranslator(0);
247
248                 if (tQCadProf->load(langFile, (*it)))
249                 {
250                         qApp->installTranslator(tQCadProf);
251                 }
252         }
253 }
254
255 /**
256  * Checks if the system has been initialized and prints a warning
257  * otherwise to stderr.
258  */
259 bool RS_System::checkInit()
260 {
261         if (!initialized)
262         {
263                 RS_DEBUG->print(RS_Debug::D_WARNING, "RS_System::checkInit: System not initialized.\n"
264                         "Use RS_SYSTEM->init(appname, appdirname) to do so.");
265         }
266
267         return initialized;
268 }
269
270 /**
271  * Creates all given directories in the user's home.
272  */
273 bool RS_System::createHomePath(const QString & p)
274 {
275         QDir dr;
276
277 //      QStringList dirs = QStringList::split('/', p, false);
278         QStringList dirs = p.split('/', QString::SkipEmptyParts);
279         QString created = getHomeDir();
280
281         for(QStringList::Iterator it=dirs.begin(); it!=dirs.end(); ++it)
282         {
283                 created += QString("/%1").arg(*it);
284
285 //              if (created.isEmpty() || QFileInfo(created).isDir() || dr.mkdir(created, true))
286                 if (created.isEmpty() || QFileInfo(created).isDir() || dr.mkdir(created))
287                 {
288                         RS_DEBUG->print("RS_System::createHomePath: Created directory '%s'",
289                                 created.toLatin1().data());
290                 }
291                 else
292                 {
293                         RS_DEBUG->print(RS_Debug::D_ERROR, "RS_System::createHomePath: Cannot create directory '%s'",
294                                 created.toLatin1().data());
295                         return false;
296                 }
297         }
298
299         return true;
300 }
301
302 /**
303  * @return Users home directory.
304  */
305 QString RS_System::getHomeDir()
306 {
307 //      return QDir::homeDirPath();
308         return QDir::homePath();
309 }
310
311 /**
312  * @return Current directory.
313  */
314 QString RS_System::getCurrentDir()
315 {
316 //      return QDir::currentDirPath();
317         return QDir::currentPath();
318 }
319
320 /**
321  * @return Application directory.
322  */
323 QString RS_System::getAppDir()
324 {
325         return appDir;
326 }
327
328 /**
329  * @return A list of absolute paths to all font files found.
330  */
331 QStringList RS_System::getFontList()
332 {
333         QStringList ret = getFileList("fonts", "cxf");
334         return ret;
335 }
336
337 /**
338  * @return A list of absolute paths to all hatch pattern files found.
339  */
340 QStringList RS_System::getPatternList()
341 {
342         QStringList ret = getFileList("patterns", "dxf");
343         return ret;
344 }
345
346 /**
347  * @return A list of absolute paths to all script files found.
348  */
349 QStringList RS_System::getScriptList()
350 {
351         QStringList ret = getFileList("scripts/qsa", "qs");
352         return ret;
353 }
354
355 /**
356  * @return A list of absolute paths to all machine configuration files found.
357  */
358 QStringList RS_System::getMachineList()
359 {
360         QStringList ret = getFileList("machines", "cxm");
361         return ret;
362 }
363
364 /**
365  * @return Absolute path to the documentation.
366  */
367 QString RS_System::getDocPath()
368 {
369         QStringList lst = getDirectoryList("doc");
370         return lst.first();
371 }
372
373 /**
374  * @return The application name.
375  */
376 QString RS_System::getAppName()
377 {
378         return appName;
379 }
380
381 /**
382  * @return The application version.
383  */
384 QString RS_System::getAppVersion()
385 {
386         return appVersion;
387 }
388
389 /**
390  * Searches for files in an application shared directory in the given
391  * subdirectory with the given extension.
392  *
393  * @return List of the absolute paths of the files found.
394  */
395 QStringList RS_System::getFileList(const QString & subDirectory, const QString & fileExtension)
396 {
397         checkInit();
398
399         /*QStringList dirList;
400
401         // Redhat style:
402         dirList.append("/usr/share/" + appDirName);
403
404         // SuSE style:
405         dirList.append("/usr/X11R6/" + appDirName);
406
407         dirList.append("/usr/X11R6/share/" + appDirName);
408         dirList.append(getHomeDir() + "/." + appDirName);
409
410         // Local directory:
411         dirList.append(".");
412         //dirList.append(getCurrentDir());
413
414         // Debian Doc:
415         /usr/share/doc/qcad/html/en/
416         */
417
418         QStringList dirList = getDirectoryList(subDirectory);
419
420         QStringList fileList;
421         QString path;
422         QDir dir;
423
424         for(QStringList::Iterator it=dirList.begin(); it!=dirList.end(); ++it)
425         {
426                 //path = QString(*it) + "/" + subDirectory;
427                 path = QString(*it);
428                 dir = QDir(path);
429
430                 if (dir.exists() && dir.isReadable())
431                 {
432 //                      QStringList files = dir.entryList("*." + fileExtension);
433                         QStringList filters;
434                         filters << "*." + fileExtension;
435                         QStringList files = dir.entryList(filters);
436
437                         for(QStringList::Iterator it2=files.begin(); it2!=files.end(); it2++)
438                                 fileList += path + "/" + (*it2);
439                 }
440         }
441
442         return fileList;
443 }
444
445 /**
446  * @return List of all directories in subdirectory 'subDirectory' in
447  * all possible QCad directories.
448  */
449 QStringList RS_System::getDirectoryList(const QString & subDirectory)
450 {
451         QStringList dirList;
452
453 #ifdef __APPLE__
454         if (subDirectory != "library")
455         {
456 #endif
457                 //local (application) directory has priority over other dirs:
458                 if (!appDir.isEmpty() && appDir != "/" && appDir != getHomeDir())
459                 {
460                         dirList.append(appDir + "/" + subDirectory);
461                 }
462
463                 // Redhat style:
464                 dirList.append("/usr/share/" + appDirName + "/" + subDirectory);
465                 // SuSE style:
466                 dirList.append("/usr/X11R6/" + appDirName + "/" + subDirectory);
467                 dirList.append("/usr/X11R6/share/" + appDirName + "/" + subDirectory);
468                 dirList.append(getHomeDir() + "/." + appDirName + "/" + subDirectory);
469 #ifdef __APPLE__
470         }
471 #endif
472
473 #ifdef __APPLE__
474         // Mac OS X - don't scan for library since this might lead us into the
475         //  wrong directory tree:
476         if (!appDir.isEmpty() && appDir != "/" /*&& subDirectory!="library"*/)
477         {
478                 dirList.append(appDir + "/../Resources/" + subDirectory);
479                 dirList.append(appDir + "/../../../" + subDirectory);
480         }
481 #endif
482
483         // Individual directories:
484         settings.beginGroup("Paths");
485
486         if (subDirectory == "fonts")
487         {
488                 dirList += settings.value("Fonts", "").toString().split(QRegExp("[;]"));
489         }
490         else if (subDirectory == "patterns")
491         {
492                 dirList += settings.value("Patterns", "").toString().split(QRegExp("[;]"));
493         }
494         else if (subDirectory.startsWith("scripts"))
495         {
496                 dirList += settings.value("Scripts", "").toString().split(QRegExp("[;]"));
497         }
498         else if (subDirectory.startsWith("library"))
499         {
500                 dirList += settings.value("Library", "").toString().split(QRegExp("[;]"));
501         }
502         else if (subDirectory.startsWith("po"))
503         {
504                 dirList += settings.value("Translations", "").toString().split(QRegExp("[;]"));
505         }
506
507         settings.endGroup();
508
509         QStringList ret;
510
511         RS_DEBUG->print("RS_System::getDirectoryList: Paths:");
512
513         for(QStringList::Iterator it=dirList.begin(); it!=dirList.end(); ++it)
514         {
515                 if (QFileInfo(*it).isDir())
516                 {
517                         ret += (*it);
518                         RS_DEBUG->print((*it).toLatin1().data());
519                 }
520         }
521
522         return ret;
523 }
524
525 QStringList RS_System::getLanguageList()
526 {
527         return languageList;
528 }
529
530 /**
531  * Converts a language string to a symbol (e.g. Deutsch or German to 'de').
532  *
533  * Supported languages: http://ftp.ics.uci.edu/pub/ietf/http/related/iso639.txt
534  */
535 QString RS_System::languageToSymbol(const QString& lang)
536 {
537     QString l = lang.toLower();
538
539     // don't use else if.. M$ visual wannabe c++ can't handle it
540     if (l == "afar")
541         {
542         return "aa";
543     }
544     if (l=="abkhazian") {
545         return "ab";
546     }
547     if (l=="afrikaans") {
548         return "af";
549     }
550     if (l=="amharic") {
551         return "am";
552     }
553     if (l=="arabic") {
554         return "ar";
555     }
556     if (l=="assamese") {
557         return "as";
558     }
559     if (l=="aymara") {
560         return "ay";
561     }
562     if (l=="azerbaijani") {
563         return "az";
564     }
565     if (l=="bashkir") {
566         return "ba";
567     }
568     if (l=="byelorussian") {
569         return "be";
570     }
571     if (l=="bulgarian") {
572         return "bg";
573     }
574     if (l=="bihari") {
575         return "bh";
576     }
577     if (l=="bislama") {
578         return "bi";
579     }
580     if (l=="bengali" || l=="bangla") {
581         return "bn";
582     }
583     if (l=="tibetan") {
584         return "bo";
585     }
586     if (l=="breton") {
587         return "br";
588     }
589     if (l=="catalan") {
590         return "ca";
591     }
592     if (l=="corsican") {
593         return "co";
594     }
595     if (l=="czech") {
596         return "cs";
597     }
598     if (l=="welsh") {
599         return "cy";
600     }
601     if (l=="danish") {
602         return "da";
603     }
604     if (l=="german" || l=="deutsch") {
605         return "de";
606     }
607     if (l=="bhutani") {
608         return "dz";
609     }
610     if (l=="greek") {
611         return "el";
612     }
613     if (l=="english") {
614         return "en";
615     }
616     if (l=="esperanto") {
617         return "eo";
618     }
619     if (l=="spanish") {
620         return "es";
621     }
622     if (l=="estonian") {
623         return "et";
624     }
625     if (l=="basque") {
626         return "eu";
627     }
628     if (l=="persian") {
629         return "fa";
630     }
631     if (l=="finnish") {
632         return "fi";
633     }
634     if (l=="fiji") {
635         return "fj";
636     }
637     if (l=="faroese") {
638         return "fo";
639     }
640     if (l=="french" || l=="francais") {
641         return "fr";
642     }
643     if (l=="frisian") {
644         return "fy";
645     }
646     if (l=="irish") {
647         return "ga";
648     }
649     if (l=="scots gaelic" || l=="gaelic") {
650         return "gd";
651     }
652     if (l=="galician") {
653         return "gl";
654     }
655     if (l=="guarani") {
656         return "gn";
657     }
658     if (l=="gujarati") {
659         return "gu";
660     }
661     if (l=="hausa") {
662         return "ha";
663     }
664     if (l=="hebrew") {
665         return "he";
666     }
667     if (l=="hindi") {
668         return "hi";
669     }
670     if (l=="croatian") {
671         return "hr";
672     }
673     if (l=="hungarian") {
674         return "hu";
675     }
676     if (l=="armenian") {
677         return "hy";
678     }
679     if (l=="interlingua") {
680         return "ia";
681     }
682     if (l=="indonesian") {
683         return "id";
684     }
685     if (l=="interlingue") {
686         return "ie";
687     }
688     if (l=="inupiak") {
689         return "ik";
690     }
691     if (l=="icelandic") {
692         return "is";
693     }
694     if (l=="italian") {
695         return "it";
696     }
697     if (l=="inuktitut") {
698         return "iu";
699     }
700     if (l=="japanese") {
701         return "ja";
702     }
703     if (l=="javanese") {
704         return "jw";
705     }
706     if (l=="georgian") {
707         return "ka";
708     }
709     if (l=="kazakh") {
710         return "kk";
711     }
712     if (l=="greenlandic") {
713         return "kl";
714     }
715     if (l=="cambodian") {
716         return "km";
717     }
718     if (l=="kannada") {
719         return "kn";
720     }
721     if (l=="korean") {
722         return "ko";
723     }
724     if (l=="kashmiri") {
725         return "ks";
726     }
727     if (l=="kurdish") {
728         return "ku";
729     }
730     if (l=="kirghiz") {
731         return "ky";
732     }
733     if (l=="latin") {
734         return "la";
735     }
736     if (l=="lingala") {
737         return "ln";
738     }
739     if (l=="laothian") {
740         return "lo";
741     }
742     if (l=="lithuanian") {
743         return "lt";
744     }
745     if (l=="latvian" || l=="lettish") {
746         return "lv";
747     }
748     if (l=="malagasy") {
749         return "mg";
750     }
751     if (l=="maori") {
752         return "mi";
753     }
754     if (l=="macedonian") {
755         return "mk";
756     }
757     if (l=="malayalam") {
758         return "ml";
759     }
760     if (l=="mongolian") {
761         return "mn";
762     }
763     if (l=="moldavian") {
764         return "mo";
765     }
766     if (l=="marathi") {
767         return "mr";
768     }
769     if (l=="malay") {
770         return "ms";
771     }
772     if (l=="maltese") {
773         return "mt";
774     }
775     if (l=="burmese") {
776         return "my";
777     }
778     if (l=="nauru") {
779         return "na";
780     }
781     if (l=="nepali") {
782         return "ne";
783     }
784     if (l=="dutch") {
785         return "nl";
786     }
787     if (l=="norwegian") {
788         return "no";
789     }
790     if (l=="occitan") {
791         return "oc";
792     }
793     if (l=="afan" || l=="oromo" || l=="afan oromo") {
794         return "om";
795     }
796     if (l=="oriya") {
797         return "or";
798     }
799     if (l=="punjabi") {
800         return "pa";
801     }
802     if (l=="polish") {
803         return "pl";
804     }
805     if (l=="pashto" || l=="pushto") {
806         return "ps";
807     }
808     if (l=="portuguese") {
809         return "pt";
810     }
811     if (l=="brasilian portuguese") {
812         return "pt-br";
813     }
814     if (l=="quechua") {
815         return "qu";
816     }
817     if (l=="rhaeto-romance") {
818         return "rm";
819     }
820     if (l=="kirundi") {
821         return "rn";
822     }
823     if (l=="romanian") {
824         return "ro";
825     }
826     if (l=="russian") {
827         return "ru";
828     }
829     if (l=="kinyarwanda") {
830         return "rw";
831     }
832     if (l=="sanskrit") {
833         return "sa";
834     }
835     if (l=="sindhi") {
836         return "sd";
837     }
838     if (l=="sangho") {
839         return "sg";
840     }
841     if (l=="serbo-Croatian") {
842         return "sh";
843     }
844     if (l=="sinhalese") {
845         return "si";
846     }
847     if (l=="slovak") {
848         return "sk";
849     }
850     if (l=="slovenian") {
851         return "sl";
852     }
853     if (l=="samoan") {
854         return "sm";
855     }
856     if (l=="shona") {
857         return "sn";
858     }
859     if (l=="somali") {
860         return "so";
861     }
862     if (l=="albanian") {
863         return "sq";
864     }
865     if (l=="serbian") {
866         return "sr";
867     }
868     if (l=="siswati") {
869         return "ss";
870     }
871     if (l=="sesotho") {
872         return "st";
873     }
874     if (l=="sundanese") {
875         return "su";
876     }
877     if (l=="swedish") {
878         return "sv";
879     }
880     if (l=="swahili") {
881         return "sw";
882     }
883     if (l=="tamil") {
884         return "ta";
885     }
886     if (l=="telugu") {
887         return "te";
888     }
889     if (l=="tajik") {
890         return "tg";
891     }
892     if (l=="thai") {
893         return "th";
894     }
895     if (l=="tigrinya") {
896         return "ti";
897     }
898     if (l=="turkmen") {
899         return "tk";
900     }
901     if (l=="tagalog") {
902         return "tl";
903     }
904     if (l=="setswana") {
905         return "tn";
906     }
907     if (l=="tonga") {
908         return "to";
909     }
910     if (l=="turkish") {
911         return "tr";
912     }
913     if (l=="tsonga") {
914         return "ts";
915     }
916     if (l=="tatar") {
917         return "tt";
918     }
919     if (l=="twi") {
920         return "tw";
921     }
922     if (l=="uighur") {
923         return "ug";
924     }
925     if (l=="ukrainian") {
926         return "uk";
927     }
928     if (l=="urdu") {
929         return "ur";
930     }
931     if (l=="uzbek") {
932         return "uz";
933     }
934     if (l=="vietnamese") {
935         return "vi";
936     }
937     if (l=="volapuk") {
938         return "vo";
939     }
940     if (l=="wolof") {
941         return "wo";
942     }
943     if (l=="xhosa") {
944         return "xh";
945     }
946     if (l=="yiddish") {
947         return "yi";
948     }
949     if (l=="yoruba") {
950         return "yo";
951     }
952     if (l=="zhuang") {
953         return "za";
954     }
955     if (l=="chinese") {
956         return "zh";
957     }
958     if (l=="zulu") {
959         return "zu";
960     }
961
962     return "";
963 }
964
965 /**
966  * Converst a language two-letter-code into a readable string
967  * (e.g. 'de' to Deutsch)
968  */
969 QString RS_System::symbolToLanguage(const QString & symb)
970 {
971         QString l = symb.toLower();
972
973         if (l == "aa")
974                 return "Afar";
975
976         if (l == "ab") {
977                 return "Abkhazian";
978         }
979         if (l == "af") {
980                 return "Afrikaans";
981         }
982         if (l == "am") {
983                 return "Amharic";
984         }
985         if (l == "ar") {
986                 return "Arabic";
987         }
988         if (l == "as") {
989                 return "Assamese";
990         }
991         if (l == "ay") {
992                 return "Aymara";
993         }
994         if (l == "az") {
995                 return "Azerbaijani";
996         }
997         if (l == "ba") {
998                 return "Bashkir";
999         }
1000         if (l == "be") {
1001                 return "Byelorussian";
1002         }
1003         if (l == "bg") {
1004                 return "Bulgarian";
1005         }
1006         if (l == "bh") {
1007                 return "Bihari";
1008         }
1009         if (l == "bi") {
1010                 return "Bislama";
1011         }
1012         if (l == "bn") {
1013                 return "Bengali";
1014         }
1015         if (l == "bo") {
1016                 return "Tibetan";
1017         }
1018         if (l == "br") {
1019                 return "Breton";
1020         }
1021         if (l == "ca") {
1022                 return "Catalan";
1023         }
1024         if (l == "co") {
1025                 return "Corsican";
1026         }
1027         if (l == "cs") {
1028                 return "Czech";
1029         }
1030         if (l == "cy") {
1031                 return "Welsh";
1032         }
1033         if (l == "da") {
1034                 return "Danish";
1035         }
1036         if (l == "de") {
1037                 return "German";
1038         }
1039         if (l == "dz") {
1040                 return "Bhutani";
1041         }
1042         if (l == "el") {
1043                 return "Greek";
1044         }
1045         if (l == "en") {
1046                 return "English";
1047         }
1048         if (l == "eo") {
1049                 return "Esperanto";
1050         }
1051         if (l == "es") {
1052                 return "Spanish";
1053         }
1054         if (l == "et") {
1055                 return "Estonian";
1056         }
1057         if (l == "eu") {
1058                 return "Basque";
1059         }
1060         if (l == "fa") {
1061                 return "Persian";
1062         }
1063         if (l == "fi") {
1064                 return "Finnish";
1065         }
1066         if (l == "fj") {
1067                 return "Fiji";
1068         }
1069         if (l == "fo") {
1070                 return "Faroese";
1071         }
1072         if (l == "fr") {
1073                 return "French";
1074         }
1075         if (l == "fy") {
1076                 return "Frisian";
1077         }
1078         if (l == "ga") {
1079                 return "Irish";
1080         }
1081         if (l == "gd") {
1082                 return "Scots Gaelic";
1083         }
1084         if (l == "gl") {
1085                 return "Galician";
1086         }
1087         if (l == "gn") {
1088                 return "Guarani";
1089         }
1090         if (l == "gu") {
1091                 return "Gujarati";
1092         }
1093         if (l == "ha") {
1094                 return "Hausa";
1095         }
1096         if (l == "he") {
1097                 return "Hebrew";
1098         }
1099         if (l == "hi") {
1100                 return "Hindi";
1101         }
1102         if (l == "hr") {
1103                 return "Croatian";
1104         }
1105         if (l == "hu") {
1106                 return "Hungarian";
1107         }
1108         if (l == "hy") {
1109                 return "Armenian";
1110         }
1111         if (l == "ia") {
1112                 return "Interlingua";
1113         }
1114         if (l == "id") {
1115                 return "Indonesian";
1116         }
1117         if (l == "ie") {
1118                 return "Interlingue";
1119         }
1120         if (l == "ik") {
1121                 return "Inupiak";
1122         }
1123         if (l == "is") {
1124                 return "Icelandic";
1125         }
1126         if (l == "it") {
1127                 return "Italian";
1128         }
1129         if (l == "iu") {
1130                 return "Inuktitut";
1131         }
1132         if (l == "ja") {
1133                 return "Japanese";
1134         }
1135         if (l == "jw") {
1136                 return "Javanese";
1137         }
1138         if (l == "ka") {
1139                 return "Georgian";
1140         }
1141         if (l == "kk") {
1142                 return "Kazakh";
1143         }
1144         if (l == "kl") {
1145                 return "Greenlandic";
1146         }
1147         if (l == "km") {
1148                 return "Cambodian";
1149         }
1150         if (l == "kn") {
1151                 return "Kannada";
1152         }
1153         if (l == "ko") {
1154                 return "Korean";
1155         }
1156         if (l == "ks") {
1157                 return "Kashmiri";
1158         }
1159         if (l == "ku") {
1160                 return "Kurdish";
1161         }
1162         if (l == "ky") {
1163                 return "Kirghiz";
1164         }
1165         if (l == "la") {
1166                 return "Latin";
1167         }
1168         if (l == "ln") {
1169                 return "Lingala";
1170         }
1171         if (l == "lo") {
1172                 return "Laothian";
1173         }
1174         if (l == "lt") {
1175                 return "Lithuanian";
1176         }
1177         if (l == "lv") {
1178                 return "Latvian";
1179         }
1180         if (l == "mg") {
1181                 return "Malagasy";
1182         }
1183         if (l == "mi") {
1184                 return "Maori";
1185         }
1186         if (l == "mk") {
1187                 return "Macedonian";
1188         }
1189         if (l == "ml") {
1190                 return "Malayalam";
1191         }
1192         if (l == "mn") {
1193                 return "Mongolian";
1194         }
1195         if (l == "mo") {
1196                 return "Moldavian";
1197         }
1198         if (l == "mr") {
1199                 return "Marathi";
1200         }
1201         if (l == "ms") {
1202                 return "Malay";
1203         }
1204         if (l == "mt") {
1205                 return "Maltese";
1206         }
1207         if (l == "my") {
1208                 return "Burmese";
1209         }
1210         if (l == "na") {
1211                 return "Nauru";
1212         }
1213         if (l == "ne") {
1214                 return "Nepali";
1215         }
1216         if (l == "nl") {
1217                 return "Dutch";
1218         }
1219         if (l == "no") {
1220                 return "Norwegian";
1221         }
1222         if (l == "oc") {
1223                 return "Occitan";
1224         }
1225         if (l == "om") {
1226                 return "Afan Oromo";
1227         }
1228         if (l == "or") {
1229                 return "Oriya";
1230         }
1231         if (l == "pa") {
1232                 return "Punjabi";
1233         }
1234         if (l == "pl") {
1235                 return "Polish";
1236         }
1237         if (l == "ps") {
1238                 return "Pashto";
1239         }
1240         if (l == "pt") {
1241                 return "Portuguese";
1242         }
1243         if (l == "pt-br") {
1244                 return "Brasilian Portuguese";
1245         }
1246         if (l == "qu") {
1247                 return "Quechua";
1248         }
1249         if (l == "rm") {
1250                 return "Rhaeto-Romance";
1251         }
1252         if (l == "rn") {
1253                 return "Kirundi";
1254         }
1255         if (l == "ro") {
1256                 return "Romanian";
1257         }
1258         if (l == "ru") {
1259                 return "Russian";
1260         }
1261         if (l == "rw") {
1262                 return "Kinyarwanda";
1263         }
1264         if (l == "sa") {
1265                 return "Sanskrit";
1266         }
1267         if (l == "sd") {
1268                 return "Sindhi";
1269         }
1270         if (l == "sg") {
1271                 return "Sangho";
1272         }
1273         if (l == "sh") {
1274                 return "Serbo-croatian";
1275         }
1276         if (l == "si") {
1277                 return "Sinhalese";
1278         }
1279         if (l == "sk") {
1280                 return "Slovak";
1281         }
1282         if (l == "sl") {
1283                 return "Slovenian";
1284         }
1285         if (l == "sm") {
1286                 return "Samoan";
1287         }
1288         if (l == "sn") {
1289                 return "Shona";
1290         }
1291         if (l == "so") {
1292                 return "Somali";
1293         }
1294         if (l == "sq") {
1295                 return "Albanian";
1296         }
1297         if (l == "sr") {
1298                 return "Serbian";
1299         }
1300         if (l == "ss") {
1301                 return "Siswati";
1302         }
1303         if (l == "st") {
1304                 return "Sesotho";
1305         }
1306         if (l == "su") {
1307                 return "Sundanese";
1308         }
1309         if (l == "sv") {
1310                 return "Swedish";
1311         }
1312         if (l == "sw") {
1313                 return "Swahili";
1314         }
1315         if (l == "ta") {
1316                 return "Tamil";
1317         }
1318         if (l == "te") {
1319                 return "Telugu";
1320         }
1321         if (l == "tg") {
1322                 return "Tajik";
1323         }
1324         if (l == "th") {
1325                 return "Thai";
1326         }
1327         if (l == "ti") {
1328                 return "Tigrinya";
1329         }
1330         if (l == "tk") {
1331                 return "Turkmen";
1332         }
1333         if (l == "tl") {
1334                 return "Tagalog";
1335         }
1336         if (l == "tn") {
1337                 return "Setswana";
1338         }
1339         if (l == "to") {
1340                 return "Tonga";
1341         }
1342         if (l == "tr") {
1343                 return "Turkish";
1344         }
1345         if (l == "ts") {
1346                 return "Tsonga";
1347         }
1348         if (l == "tt") {
1349                 return "Tatar";
1350         }
1351         if (l == "tw") {
1352                 return "Twi";
1353         }
1354         if (l == "ug") {
1355                 return "Uighur";
1356         }
1357         if (l == "uk") {
1358                 return "Ukrainian";
1359         }
1360         if (l == "ur") {
1361                 return "Urdu";
1362         }
1363         if (l == "uz") {
1364                 return "Uzbek";
1365         }
1366         if (l == "vi") {
1367                 return "Vietnamese";
1368         }
1369         if (l == "vo") {
1370                 return "Volapuk";
1371         }
1372         if (l == "wo") {
1373                 return "Wolof";
1374         }
1375         if (l == "xh") {
1376                 return "Xhosa";
1377         }
1378         if (l == "yi") {
1379                 return "Yiddish";
1380         }
1381
1382         if (l == "yo")
1383                 return "Yoruba";
1384
1385         if (l == "za")
1386                 return "Zhuang";
1387
1388         if (l == "zh")
1389                 return "Chinese";
1390
1391         if (l == "zu")
1392                 return "Zulu";
1393
1394         return "";
1395 }
1396
1397 /**
1398  * Tries to convert the given encoding string to an encoding Qt knows.
1399  */
1400 QString RS_System::getEncoding(const QString & str)
1401 {
1402     QString l=str.toLower();
1403
1404     if (l=="latin1" || l=="ansi_1252" || l=="iso-8859-1" ||
1405             l=="cp819" || l=="csiso" || l=="ibm819" || l=="iso_8859-1" ||
1406             l=="iso8859-1" || l=="iso-ir-100" || l=="l1") {
1407         return "Latin1";
1408     } else if (l=="big5" || l=="ansi_950" || l=="cn-big5" || l=="csbig5" ||
1409                l=="x-x-big5") {
1410         return "Big5";
1411     } else if (l=="big5-hkscs") {
1412         return "Big5-HKSCS";
1413     } else if (l=="eucjp" || l=="euc-jp" || l=="cseucpkdfmtjapanese" ||
1414                l=="x-euc" || l=="x-euc-jp") {
1415         return "eucJP";
1416     } else if (l=="euckr") {
1417         return "eucKR";
1418     } else if (l=="gb2312" || l=="gb2312" || l=="chinese" || l=="cn-gb" ||
1419                l=="csgb2312" || l=="csgb231280" || l=="csiso58gb231280" ||
1420                l=="gb_2312-80" || l=="gb231280" || l=="gb2312-80" || l=="gbk" ||
1421                l=="iso-ir-58") {
1422         return "GB2312";
1423     } else if (l=="gbk") {
1424         return "GBK";
1425     } else if (l=="gb18030") {
1426         return "GB18030";
1427     } else if (l=="jis7") {
1428         return "JIS7";
1429     } else if (l=="shift-jis" || l=="ansi_932" || l=="shift_jis" || l=="csShiftJIS" ||
1430                l=="cswindows31j" || l=="ms_kanji" || l=="x-ms-cp932" || l=="x-sjis") {
1431         return "Shift-JIS";
1432     } else if (l=="tscii") {
1433         return "TSCII";
1434     } else if (l=="utf88-bit") {
1435         return "utf88-bit";
1436     } else if (l=="utf16") {
1437         return "utf16";
1438     } else if (l=="koi8-r") {
1439         return "KOI8-R";
1440     } else if (l=="koi8-u") {
1441         return "KOI8-U";
1442     } else if (l=="iso8859-1") {
1443         return "ISO8859-1";
1444     } else if (l=="iso8859-2") {
1445         return "ISO8859-2";
1446     } else if (l=="iso8859-3") {
1447         return "ISO8859-3";
1448     } else if (l=="iso8859-4" || l=="ansi_1257") {
1449         return "ISO8859-4";
1450     } else if (l=="iso8859-5") {
1451         return "ISO8859-5";
1452     } else if (l=="iso8859-6" || l=="ansi_1256") {
1453         return "ISO8859-6";
1454     } else if (l=="iso8859-7" || l=="ansi_1253") {
1455         return "ISO8859-7";
1456     } else if (l=="iso8859-8") {
1457         return "ISO8859-8";
1458     } else if (l=="iso8859-8-i" || l=="ansi_1255") {
1459         return "ISO8859-8-i";
1460     } else if (l=="iso8859-9" || l=="ansi_1254") {
1461         return "ISO8859-9";
1462     } else if (l=="iso8859-10") {
1463         return "ISO8859-10";
1464     } else if (l=="iso8859-13") {
1465         return "ISO8859-13";
1466     } else if (l=="iso8859-14") {
1467         return "ISO8859-14";
1468     } else if (l=="iso8859-15") {
1469         return "ISO8859-15";
1470     } else if (l=="ibm 850") {
1471         return "IBM 850";
1472     } else if (l=="ibm 866") {
1473         return "IBM 866";
1474     } else if (l=="cp874") {
1475         return "CP874";
1476     } else if (l=="cp1250") {
1477         return "CP1250";
1478     } else if (l=="cp1251") {
1479         return "CP1251";
1480     } else if (l=="cp1252") {
1481         return "CP1252";
1482     } else if (l=="cp1253") {
1483         return "CP1253";
1484     } else if (l=="cp1254") {
1485         return "CP1254";
1486     } else if (l=="cp1255") {
1487         return "CP1255";
1488     } else if (l=="cp1256") {
1489         return "CP1256";
1490     } else if (l=="cp1257") {
1491         return "CP1257";
1492     } else if (l=="cp1258") {
1493         return "CP1258";
1494     } else if (l=="apple roman") {
1495         return "Apple Roman";
1496     } else if (l=="tis-620") {
1497         return "TIS-620";
1498     }
1499
1500     return "latin1";
1501 }
1502
1503 /** Returns ISO code for given locale. Needed for win32 to convert
1504  from system encodings.
1505  Locale names mostly copied from XFree86.
1506
1507  The code may be incomplete (chinese/japanese locales, etc.)
1508
1509  2004-05-13, J Staniek
1510 */
1511 static QMap<QString, QString> loc_map;
1512
1513 QString RS_System::localeToISO(const QString & locale)
1514 {
1515         if (loc_map.isEmpty())
1516         {
1517                 loc_map["croatian"]="ISO8859-2";
1518                 loc_map["cs"]="ISO8859-2";
1519                 loc_map["cs_CS"]="ISO8859-2";
1520                 loc_map["cs_CZ"]="ISO8859-2";
1521                 loc_map["cz"]="ISO8859-2";
1522                 loc_map["cz_CZ"]="ISO8859-2";
1523                 loc_map["czech"]="ISO8859-2";
1524                 loc_map["hr"]="ISO8859-2";
1525                 loc_map["hr_HR"]="ISO8859-2";
1526                 loc_map["hu"]="ISO8859-2";
1527                 loc_map["hu_HU"]="ISO8859-2";
1528                 loc_map["hungarian"]="ISO8859-2";
1529                 loc_map["pl"]="ISO8859-2";
1530                 loc_map["pl_PL"]="ISO8859-2";
1531                 loc_map["polish"]="ISO8859-2";
1532                 loc_map["ro"]="ISO8859-2";
1533                 loc_map["ro_RO"]="ISO8859-2";
1534                 loc_map["rumanian"]="ISO8859-2";
1535                 loc_map["serbocroatian"]="ISO8859-2";
1536                 loc_map["sh"]="ISO8859-2";
1537                 loc_map["sh_SP"]="ISO8859-2";
1538                 loc_map["sh_YU"]="ISO8859-2";
1539                 loc_map["sk"]="ISO8859-2";
1540                 loc_map["sk_SK"]="ISO8859-2";
1541                 loc_map["sl"]="ISO8859-2";
1542                 loc_map["sl_CS"]="ISO8859-2";
1543                 loc_map["sl_SI"]="ISO8859-2";
1544                 loc_map["slovak"]="ISO8859-2";
1545                 loc_map["slovene"]="ISO8859-2";
1546                 loc_map["sr_SP"]="ISO8859-2";
1547
1548                 loc_map["eo"]="ISO8859-3";
1549
1550                 loc_map["ee"]="ISO8859-4";
1551                 loc_map["ee_EE"]="ISO8859-4";
1552
1553                 loc_map["mk"]="ISO8859-5";
1554                 loc_map["mk_MK"]="ISO8859-5";
1555                 loc_map["sp"]="ISO8859-5";
1556                 loc_map["sp_YU"]="ISO8859-5";
1557
1558                 loc_map["ar_AA"]="ISO8859-6";
1559                 loc_map["ar_SA"]="ISO8859-6";
1560                 loc_map["arabic"]="ISO8859-6";
1561
1562                 loc_map["el"]="ISO8859-7";
1563                 loc_map["el_GR"]="ISO8859-7";
1564                 loc_map["greek"]="ISO8859-7";
1565
1566                 loc_map["hebrew"]="ISO8859-8";
1567                 loc_map["he"]="ISO8859-8";
1568                 loc_map["he_IL"]="ISO8859-8";
1569                 loc_map["iw"]="ISO8859-8";
1570                 loc_map["iw_IL"]="ISO8859-8";
1571
1572                 loc_map["tr"]="ISO8859-9";
1573                 loc_map["tr_TR"]="ISO8859-9";
1574                 loc_map["turkish"]="ISO8859-9";
1575
1576                 loc_map["lt"]="ISO8859-13";
1577                 loc_map["lt_LT"]="ISO8859-13";
1578                 loc_map["lv"]="ISO8859-13";
1579                 loc_map["lv_LV"]="ISO8859-13";
1580
1581                 loc_map["et"]="ISO8859-15";
1582                 loc_map["et_EE"]="ISO8859-15";
1583                 loc_map["br_FR"]="ISO8859-15";
1584                 loc_map["ca_ES"]="ISO8859-15";
1585                 loc_map["de"]="ISO8859-15";
1586                 loc_map["de_AT"]="ISO8859-15";
1587                 loc_map["de_BE"]="ISO8859-15";
1588                 loc_map["de_DE"]="ISO8859-15";
1589                 loc_map["de_LU"]="ISO8859-15";
1590                 loc_map["en_IE"]="ISO8859-15";
1591                 loc_map["es"]="ISO8859-15";
1592                 loc_map["es_ES"]="ISO8859-15";
1593                 loc_map["eu_ES"]="ISO8859-15";
1594                 loc_map["fi"]="ISO8859-15";
1595                 loc_map["fi_FI"]="ISO8859-15";
1596                 loc_map["finnish"]="ISO8859-15";
1597                 loc_map["fr"]="ISO8859-15";
1598                 loc_map["fr_FR"]="ISO8859-15";
1599                 loc_map["fr_BE"]="ISO8859-15";
1600                 loc_map["fr_LU"]="ISO8859-15";
1601                 loc_map["french"]="ISO8859-15";
1602                 loc_map["ga_IE"]="ISO8859-15";
1603                 loc_map["gl_ES"]="ISO8859-15";
1604                 loc_map["it"]="ISO8859-15";
1605                 loc_map["it_IT"]="ISO8859-15";
1606                 loc_map["oc_FR"]="ISO8859-15";
1607                 loc_map["nl"]="ISO8859-15";
1608                 loc_map["nl_BE"]="ISO8859-15";
1609                 loc_map["nl_NL"]="ISO8859-15";
1610                 loc_map["pt"]="ISO8859-15";
1611                 loc_map["pt_PT"]="ISO8859-15";
1612                 loc_map["sv_FI"]="ISO8859-15";
1613                 loc_map["wa_BE"]="ISO8859-15";
1614
1615                 loc_map["uk"]="KOI8-U";
1616                 loc_map["uk_UA"]="KOI8-U";
1617                 loc_map["ru_YA"]="KOI8-U";
1618                 loc_map["ukrainian"]="KOI8-U";
1619
1620                 loc_map["be"]="KOI8-R";
1621                 loc_map["be_BY"]="KOI8-R";
1622                 loc_map["bg"]="KOI8-R";
1623                 loc_map["bg_BG"]="KOI8-R";
1624                 loc_map["bulgarian"]="KOI8-R";
1625                 loc_map["ba_RU"]="KOI8-R";
1626                 loc_map["ky"]="KOI8-R";
1627                 loc_map["ky_KG"]="KOI8-R";
1628                 loc_map["kk"]="KOI8-R";
1629                 loc_map["kk_KZ"]="KOI8-R";
1630         }
1631
1632         QString l = loc_map[locale];
1633
1634         if (l.isEmpty())
1635                 return "ISO8859-1";
1636
1637         return l;
1638 }