NCL Composer  0.1.5
 All Classes Functions Variables Pages
composer-gui/main.cpp
1 /* Copyright (c) 2011 Telemidia/PUC-Rio.
2  * All rights reserved. This program and the accompanying materials
3  * are made available under the terms of the Eclipse Public License v1.0
4  * which accompanies this distribution, and is available at
5  * http://www.eclipse.org/legal/epl-v10.html
6  *
7  * Contributors:
8  * Telemidia/PUC-Rio - initial API and implementation
9  */
10 #include <QtGui/QApplication>
11 #include <QApplication>
12 #include <QResource>
13 #include <QObject>
14 #include <QStringList>
15 
16 #include <core/util/ComposerSettings.h>
17 
18 #include "ComposerMainWindow.h"
19 
20 #ifdef Q_WS_X11
21 #include <QX11EmbedWidget>
22 #include <X11/Xlib.h>
23 #endif
24 
25 using namespace composer::gui;
26 
30 void updateSettingsWithDefaults()
31 {
32 /* Defaults plugins paths */
33  QStringList defaultPluginsPath;
34 
35  // The first path will look for plugins is at user's home.
36  defaultPluginsPath << QDir::homePath() + QString("/composer/extensions");
37 
38  // After that we will look for plugins in the default system path
39 #ifdef Q_WS_MAC
40  defaultPluginsPath << "/Library/Application Support/Composer/Extensions"
41  << QApplication::applicationDirPath() +
42  "/../PlugIns/composer";
43 #elif defined(Q_WS_WIN32)
44  defaultPluginsPath << QApplication::applicationDirPath() + "/extensions";
45  defaultPluginsPath << "C:/Composer/extensions";
46 #else
47  // PREFIX Should be defined by the qmake while compiling the source code.
48 #ifdef EXT_DEFAULT_PATH
49  defaultPluginsPath << QString(EXT_DEFAULT_PATH)
50  + QString("/lib/composer/extensions");
51 #endif
52 
53 #endif
54 
55  GlobalSettings settings;
56  settings.beginGroup("extensions");
57  QStringList extensions_path = settings.value("path").toStringList();
58  extensions_path << defaultPluginsPath; //add default to extensions path
59  extensions_path.removeDuplicates();
60  settings.setValue("path", extensions_path); //udpate with the new value
61  settings.endGroup();
62 /* End defaults plugin path */
63 
64 /* Default language */
65  settings.beginGroup("languages");
66  if(!settings.contains("currentLanguage"))
67  settings.setValue("currentLanguage", "en");
68 
69  // Set the defaults supported languages
70  if(!settings.contains("supportedLanguages"))
71  {
72  QStringList list;
73  list << "en_US" << "pt_BR" << "es_ES";
74  settings.setValue("supportedLanguages", list);
75  }
76  settings.endGroup();
77 /* End default language */
78 
79 /* Import Bases */
80  QString defaultConnBaseDir;
81  settings.beginGroup("importBases");
82  if(!settings.contains("default_connector_base"))
83  {
84  #ifdef Q_WS_MAC
85  defaultConnBaseDir = "/Library/Application Support/Composer/Data/";
86  #elif defined(Q_WS_WIN32)
87  defaultConnBaseDir = QApplication::applicationDirPath() + "/data/";
88  #else
89  // PREFIX Should be defined by the qmake while compiling the source code.
90  #ifdef EXT_DEFAULT_PATH
91  defaultConnBaseDir = QString(EXT_DEFAULT_PATH)
92  + QString("/share/composer/");
93  #endif
94  #endif
95  }
96  settings.setValue("default_conn_base",
97  defaultConnBaseDir + "defaultConnBase.ncl");
98  settings.endGroup();
99 /*End Import Bases*/
100 
101 /* Stylesheets */
102  QStringList stylesheetsDirs =
103  settings.value("default_stylesheets_dirs").toStringList();
104 
105  stylesheetsDirs << QString(DATA_PATH);
106 
107 #ifdef Q_WS_WIN32
108  stylesheetsDirs << QApplication::applicationDirPath() + "/data";
109 #elif defined(Q_WS_MAC)
110  stylesheetsDirs << QApplication::applicationDirPath() + "/../PlugIns/composer";
111 #endif
112 
113  stylesheetsDirs.removeDuplicates();
114 
115  settings.setValue("default_stylesheets_dirs", stylesheetsDirs);
116 /* End Stylesheets */
117 }
118 
119 void loadTranslations(QApplication *app)
120 {
121  /* Get the current language code */
122  GlobalSettings settings;
123  settings.beginGroup("languages");
124  QString language_code = settings.value("currentLanguage",
125  QString("en")).toString();
126  settings.endGroup();
127  qDebug() << "[GUI] Current Language = " << language_code;
128 
129  QLocale locale = QLocale(language_code);
130  QLocale::setDefault(locale);
131 
132  /* Get all paths were can be translations */
133  settings.beginGroup("extensions");
134  QStringList extensions_paths = settings.value("path").toStringList();
135  settings.endGroup();
136 
137  qDebug() << "[GUI]" << extensions_paths;
138 
139  /* Go in each path and search for files from that language */
140  foreach(QString curPath, extensions_paths)
141  {
142  qDebug() << curPath;
143  QDir curDir(curPath);
144  //filter only the files for the current language code
145  curDir.setNameFilters(QStringList() << "*_" + language_code + ".qm");
146 
147  QFileInfoList fileInfoList = curDir.entryInfoList();
148 
149  // for each translation file install in the application.
150  foreach(QFileInfo fileInfo, fileInfoList)
151  {
152  qDebug() << "[GUI] translation file = " << fileInfo.absoluteFilePath();
153  QTranslator *composerTranslator = new QTranslator(qApp);
154  composerTranslator->load(fileInfo.absoluteFilePath());
155  QCoreApplication::installTranslator(composerTranslator);
156  }
157  }
158 }
159 
160 
161 int main(int argc, char *argv[])
162 {
163 #ifdef Q_WS_X11
164 XInitThreads();
165 #endif
166  QApplication a(argc, argv);
167  a.setQuitOnLastWindowClosed(true);
168 
169  updateSettingsWithDefaults();
170  loadTranslations(&a);
171 
172  QResource::registerResource("images.qrc");
173  QCoreApplication::setOrganizationName("Telemidia Lab");
174  QCoreApplication::setOrganizationDomain("telemidia.pucrio.br");
175  QCoreApplication::setApplicationName("composer");
176 
177  //make the library search path include the application dir on windows
178  //this is so the plugins can find the dlls they are linked to at run time
179  QApplication::addLibraryPath(QApplication::applicationDirPath());
180 
182  w.setWindowIcon(QIcon(":/mainwindow/icon"));
183 
184  QStringList dirs =
185  GlobalSettings().value("default_stylesheets_dirs").toStringList();
186 
187  foreach(QString dir, dirs)
188  {
189  if (QFile::exists(dir+"/style.qss"))
190  {
191  QFile style(dir+"/style.qss");
192  if (style.open(QFile::ReadOnly))
193  {
194  w.setStyleSheet(style.readAll());
195  style.close();
196  }
197  break;
198  }
199  }
200 
201  w.init(a);
202 
203  QStringList argList = a.arguments();
204  QStringList filesToOpen;
205  for(int i = 0; i < argList.size(); i++)
206  {
207  if(argList.at(i).endsWith(".cpr"))
208  filesToOpen << argList.at(i);
209  }
210 
211  w.openProjects(filesToOpen);
212 
213  return a.exec();
214 }