source: calamares/trunk/fuentes/src/calamares/CalamaresApplication.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 11.8 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18#include <QDesktopWidget>
19#include "CalamaresApplication.h"
20
21#include "CalamaresConfig.h"
22#include "CalamaresWindow.h"
23#include "CalamaresVersion.h"
24#include "progresstree/ProgressTreeView.h"
25#include "progresstree/ProgressTreeModel.h"
26
27#include "modulesystem/ModuleManager.h"
28#include "utils/CalamaresUtilsGui.h"
29#include "utils/CalamaresUtilsSystem.h"
30#include "utils/Logger.h"
31#include "JobQueue.h"
32#include "Branding.h"
33#include "Settings.h"
34#include "viewpages/ViewStep.h"
35#include "ViewManager.h"
36
37#include <QDir>
38#include <QFileInfo>
39
40
41CalamaresApplication::CalamaresApplication( int& argc, char* argv[] )
42    : QApplication( argc, argv )
43    , m_mainwindow( nullptr )
44    , m_moduleManager( nullptr )
45    , m_debugMode( false )
46{
47    // Setting the organization name makes the default cache
48    // directory -- where Calamares stores logs, for instance --
49    // <org>/<app>/, so we end up with ~/.cache/Calamares/calamares/
50    // which is excessively squidly.
51    //
52    // setOrganizationName( QLatin1String( CALAMARES_ORGANIZATION_NAME ) );
53    setOrganizationDomain( QLatin1String( CALAMARES_ORGANIZATION_DOMAIN ) );
54    setApplicationName( QLatin1String( CALAMARES_APPLICATION_NAME ) );
55    setApplicationVersion( QLatin1String( CALAMARES_VERSION ) );
56
57    cDebug() << "Calamares version:" << CALAMARES_VERSION;
58
59    CalamaresUtils::installTranslator( QLocale::system(), QString(), this );
60
61    QFont f = font();
62
63    cDebug() << "Default font size" << f.pointSize() << ';' << f.pixelSize() << "px";
64    CalamaresUtils::setDefaultFontSize( f.pointSize() );
65
66    cDebug() << "Available languages:" << QString( CALAMARES_TRANSLATION_LANGUAGES ).split( ';' );
67}
68
69
70void
71CalamaresApplication::init()
72{
73    Logger::setupLogfile();
74
75    setQuitOnLastWindowClosed( false );
76
77    initQmlPath();
78    initSettings();
79    initBranding();
80
81    setWindowIcon( QIcon( Calamares::Branding::instance()->
82                          imagePath( Calamares::Branding::ProductIcon ) ) );
83
84    cDebug() << "STARTUP: initQmlPath, initSettings, initBranding done";
85
86    initModuleManager(); //also shows main window
87
88    cDebug() << "STARTUP: initModuleManager: module init started";
89}
90
91
92CalamaresApplication::~CalamaresApplication()
93{
94    cDebug( Logger::LOGVERBOSE ) << "Shutting down Calamares...";
95
96//    if ( JobQueue::instance() )
97//        JobQueue::instance()->stop();
98
99//    delete m_mainwindow;
100
101//    delete JobQueue::instance();
102
103    cDebug( Logger::LOGVERBOSE ) << "Finished shutdown.";
104}
105
106
107CalamaresApplication*
108CalamaresApplication::instance()
109{
110    return qobject_cast< CalamaresApplication* >( QApplication::instance() );
111}
112
113
114void
115CalamaresApplication::setDebug( bool enabled )
116{
117    m_debugMode = enabled;
118}
119
120
121bool
122CalamaresApplication::isDebug()
123{
124    return m_debugMode;
125}
126
127
128CalamaresWindow*
129CalamaresApplication::mainWindow()
130{
131    return m_mainwindow;
132}
133
134
135void
136CalamaresApplication::initQmlPath()
137{
138    QDir importPath;
139
140    QString subpath( "qml" );
141
142    if ( CalamaresUtils::isAppDataDirOverridden() )
143    {
144        importPath = QDir( CalamaresUtils::appDataDir()
145                           .absoluteFilePath( subpath ) );
146        if ( !importPath.exists() || !importPath.isReadable() )
147        {
148            cError() << "FATAL: explicitly configured application data directory"
149                   << CalamaresUtils::appDataDir().absolutePath()
150                   << "does not contain a valid QML modules directory at"
151                   << importPath.absolutePath()
152                   << "\nCowardly refusing to continue startup without the QML directory.";
153            ::exit( EXIT_FAILURE );
154        }
155    }
156    else
157    {
158        QStringList qmlDirCandidatesByPriority;
159        if ( isDebug() )
160        {
161            qmlDirCandidatesByPriority.append(
162                        QDir::current().absoluteFilePath(
163                        QString( "src/%1" )
164                            .arg( subpath ) ) );
165        }
166        qmlDirCandidatesByPriority.append( CalamaresUtils::appDataDir()
167                            .absoluteFilePath( subpath ) );
168
169        foreach ( const QString& path, qmlDirCandidatesByPriority )
170        {
171            QDir dir( path );
172            if ( dir.exists() && dir.isReadable() )
173            {
174                importPath = dir;
175                break;
176            }
177        }
178
179        if ( !importPath.exists() || !importPath.isReadable() )
180        {
181            cError() << "FATAL: none of the expected QML paths ("
182                   << qmlDirCandidatesByPriority.join( ", " )
183                   << ") exist."
184                   << "\nCowardly refusing to continue startup without the QML directory.";
185            ::exit( EXIT_FAILURE );
186        }
187    }
188
189    CalamaresUtils::setQmlModulesDir( importPath );
190}
191
192
193void
194CalamaresApplication::initSettings()
195{
196    QFileInfo settingsFile;
197    if ( CalamaresUtils::isAppDataDirOverridden() )
198    {
199        settingsFile = QFileInfo( CalamaresUtils::appDataDir().absoluteFilePath( "settings.conf" ) );
200        if ( !settingsFile.exists() || !settingsFile.isReadable() )
201        {
202            cError() << "FATAL: explicitly configured application data directory"
203                   << CalamaresUtils::appDataDir().absolutePath()
204                   << "does not contain a valid settings.conf file."
205                   << "\nCowardly refusing to continue startup without settings.";
206            ::exit( EXIT_FAILURE );
207        }
208    }
209    else
210    {
211        QStringList settingsFileCandidatesByPriority;
212        if ( isDebug() )
213        {
214            settingsFileCandidatesByPriority.append(
215                QDir::currentPath() +
216                QDir::separator() +
217                "settings.conf" );
218        }
219        settingsFileCandidatesByPriority.append( CMAKE_INSTALL_FULL_SYSCONFDIR "/calamares/settings.conf" );
220        settingsFileCandidatesByPriority.append( CalamaresUtils::appDataDir()
221                                                    .absoluteFilePath( "settings.conf" ) );
222
223        foreach ( const QString& path, settingsFileCandidatesByPriority )
224        {
225            QFileInfo pathFi( path );
226            if ( pathFi.exists() && pathFi.isReadable() )
227            {
228                settingsFile = pathFi;
229                break;
230            }
231        }
232
233        if ( !settingsFile.exists() || !settingsFile.isReadable() )
234        {
235            cError() << "FATAL: none of the expected configuration file paths ("
236                   << settingsFileCandidatesByPriority.join( ", " )
237                   << ") contain a valid settings.conf file."
238                   << "\nCowardly refusing to continue startup without settings.";
239            ::exit( EXIT_FAILURE );
240        }
241    }
242
243    new Calamares::Settings( settingsFile.absoluteFilePath(), isDebug(), this );
244}
245
246
247void
248CalamaresApplication::initBranding()
249{
250    QString brandingComponentName = Calamares::Settings::instance()->brandingComponentName();
251    if ( brandingComponentName.simplified().isEmpty() )
252    {
253        cError() << "FATAL: branding component not set in settings.conf";
254        ::exit( EXIT_FAILURE );
255    }
256
257    QString brandingDescriptorSubpath = QString( "branding/%1/branding.desc" )
258                                        .arg( brandingComponentName );
259
260    QFileInfo brandingFile;
261    if ( CalamaresUtils::isAppDataDirOverridden() )
262    {
263        brandingFile = QFileInfo( CalamaresUtils::appDataDir()
264                                  .absoluteFilePath( brandingDescriptorSubpath ) );
265        if ( !brandingFile.exists() || !brandingFile.isReadable() )
266        {
267            cError() << "FATAL: explicitly configured application data directory"
268                   << CalamaresUtils::appDataDir().absolutePath()
269                   << "does not contain a valid branding component descriptor at"
270                   << brandingFile.absoluteFilePath()
271                   << "\nCowardly refusing to continue startup without branding.";
272            ::exit( EXIT_FAILURE );
273        }
274    }
275    else
276    {
277        QStringList brandingFileCandidatesByPriority;
278        if ( isDebug() )
279        {
280            brandingFileCandidatesByPriority.append(
281                QDir::currentPath() +
282                QDir::separator() +
283                "src" +
284                QDir::separator() +
285                brandingDescriptorSubpath );
286        }
287        brandingFileCandidatesByPriority.append( QDir( CMAKE_INSTALL_FULL_SYSCONFDIR "/calamares/" )
288                                                 .absoluteFilePath( brandingDescriptorSubpath ) );
289        brandingFileCandidatesByPriority.append( CalamaresUtils::appDataDir()
290                                                 .absoluteFilePath( brandingDescriptorSubpath ) );
291
292        foreach ( const QString& path, brandingFileCandidatesByPriority )
293        {
294            QFileInfo pathFi( path );
295            if ( pathFi.exists() && pathFi.isReadable() )
296            {
297                brandingFile = pathFi;
298                break;
299            }
300        }
301
302        if ( !brandingFile.exists() || !brandingFile.isReadable() )
303        {
304            cError() << "FATAL: none of the expected branding descriptor file paths ("
305                   << brandingFileCandidatesByPriority.join( ", " )
306                   << ") contain a valid branding.desc file."
307                   << "\nCowardly refusing to continue startup without branding.";
308            ::exit( EXIT_FAILURE );
309        }
310    }
311
312    new Calamares::Branding( brandingFile.absoluteFilePath(), this );
313}
314
315
316void
317CalamaresApplication::initModuleManager()
318{
319    m_moduleManager = new Calamares::ModuleManager(
320        Calamares::Settings::instance()->modulesSearchPaths(), this );
321    connect( m_moduleManager, &Calamares::ModuleManager::initDone,
322             this,            &CalamaresApplication::initView );
323    m_moduleManager->init();
324}
325
326
327void
328CalamaresApplication::initView()
329{
330    cDebug() << "STARTUP: initModuleManager: all modules init done";
331    initJobQueue();
332    cDebug() << "STARTUP: initJobQueue done";
333
334    m_mainwindow = new CalamaresWindow(); //also creates ViewManager
335
336    connect( m_moduleManager, &Calamares::ModuleManager::modulesLoaded,
337             this, &CalamaresApplication::initViewSteps );
338    connect( m_moduleManager, &Calamares::ModuleManager::modulesFailed,
339             this, &CalamaresApplication::initFailed );
340
341    m_moduleManager->loadModules();
342
343    m_mainwindow->move(
344        this->desktop()->availableGeometry().center() -
345        m_mainwindow->rect().center() );
346
347    cDebug() << "STARTUP: CalamaresWindow created; loadModules started";
348}
349
350
351void
352CalamaresApplication::initViewSteps()
353{
354    cDebug() << "STARTUP: loadModules for all modules done";
355    m_mainwindow->show();
356    ProgressTreeModel* m = new ProgressTreeModel( nullptr );
357    ProgressTreeView::instance()->setModel( m );
358    cDebug() << "STARTUP: Window now visible and ProgressTreeView populated";
359}
360
361void
362CalamaresApplication::initFailed(const QStringList& l)
363{
364    cError() << "STARTUP: failed modules are" << l;
365    m_mainwindow->show();
366}
367
368void
369CalamaresApplication::initJobQueue()
370{
371    Calamares::JobQueue* jobQueue = new Calamares::JobQueue( this );
372    new CalamaresUtils::System( Calamares::Settings::instance()->doChroot(), this );
373    Calamares::Branding::instance()->setGlobals( jobQueue->globalStorage() );
374}
Note: See TracBrowser for help on using the repository browser.