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