summaryrefslogtreecommitdiffstats
path: root/umbrello/umbrello/main.cpp
blob: ffbf3e9feae14121fde1044de1cb88c0efebcb09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   copyright (C) 2002-2006                                               *
 *   Umbrello UML Modeller Authors <uml-devel@uml.sf.net>                  *
 ***************************************************************************/

#include <unistd.h>

// kde includes
#include <tdeaboutdata.h>
#include <tdeapplication.h>
#include <tdecmdlineargs.h>
#include <tdeconfig.h>
#include <klocale.h>
#include <ktip.h>
#include <kdebug.h>
#include <twin.h>

// app includes
#include "uml.h"
#include "version.h"
#include "umldoc.h"
#include "cmdlineexportallviewsevent.h"
#include "kstartuplogo.h"

static const char description[] =
    I18N_NOOP("Umbrello UML Modeller");
// INSERT A DESCRIPTION FOR YOUR APPLICATION HERE

/**
 * @todo Add options to use the documentation generators from command line.
 */
static TDECmdLineOptions options[] =
    {
        { "+[File]", I18N_NOOP("File to open"), 0 },
        { "export <extension>", I18N_NOOP("export diagrams to extension and exit"), 0},
        { "directory <url>", I18N_NOOP("the local directory to save the exported diagrams in"), I18N_NOOP("the directory of the file")},
        { "use-folders", I18N_NOOP("keep the tree structure used to store the views in the document in the target directory"), 0},
        // INSERT YOUR COMMANDLINE OPTIONS HERE
        TDECmdLineLastOption
    };

/**
 * Determines if the application GUI should be shown based on command line arguments.
 *
 * @param args The command line arguments given.
 * @return True if the GUI should be shown, false otherwise.
 */
bool getShowGUI(TDECmdLineArgs *args);

/**
 * Creates, shows and returns the startup logo for the application if it should be shown,
 * else returns null pointer.
 * The startup logo is shown if it is configured to be shown and also the GUI must be shown.
 *
 * @param cfg The application configuration.
 * @param showGUI If the GUI should be shown.
 * @return The startup logo for the application, or a null pointer if it shouldn't be shown.
 */
TDEStartupLogo* showStartupLogo(TDEConfig* cfg, bool showGUI);

/**
 * Initializes the document used by the application.
 * If a file was specified in command line arguments, opens that file. Else, it
 * opens the last opened file, or a new file if there isn't any "last file used"
 * in the configuration.
 *
 * @param args The command line arguments given.
 * @param cfg The application configuration.
 */
void initDocument(TDECmdLineArgs *args, TDEConfig* cfg);

/**
 * Export all the views in the document using the command line args set by the user.
 * Errors that occurred while exporting, if any, are shown using kError().
 *
 * @param args The command line arguments given.
 * @param exportOpt A list containing all the "export" arguments given.
 */
void exportAllViews(TDECmdLineArgs *args, const QCStringList &exportOpt);

extern "C" int flushEvents() {
    kapp->processEvents();
    return 0;
}

int main(int argc, char *argv[]) {
    TDEAboutData aboutData( "umbrello", I18N_NOOP("Umbrello UML Modeller"),
                          UMBRELLO_VERSION, description, TDEAboutData::License_GPL,
                          I18N_NOOP("(c) 2001 Paul Hensgen, (c) 2002-2006 Umbrello UML Modeller Authors"), 0,
                          "http://uml.sf.net/");
    aboutData.addAuthor("Paul Hensgen",0, "phensgen@users.sourceforge.net");
    aboutData.addAuthor(I18N_NOOP("Umbrello UML Modeller Authors"), 0, "uml-devel@lists.sourceforge.net");
    TDECmdLineArgs::init( argc, argv, &aboutData );
    TDECmdLineArgs::addCmdLineOptions( options ); // Add our own options.

    TDEApplication app;
    if( app.isRestored() ) {
        RESTORE( UMLApp );
    } else {
        TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
        bool showGUI = getShowGUI(args);

        UMLApp *uml = new UMLApp();
        flushEvents();
        TDEConfig * cfg = app.config();

        TDEStartupLogo* startLogo = showStartupLogo(cfg, showGUI);

        if (showGUI) {
            uml->show();
        }
        uml->initGenerator();

        //show tips if wanted
        if (showGUI) {
            KTipDialog::showTip();
        }

        initDocument(args, cfg);

        // export option
        QCStringList exportOpt = args->getOptionList("export");
        if (exportOpt.size() > 0) {
             exportAllViews(args, exportOpt);
        }

        if ( showGUI && (startLogo != 0L) && !startLogo->isHidden() ) {
            startLogo->raise();
        }
    }
    return app.exec();
}

bool getShowGUI(TDECmdLineArgs *args) {
    if (args->getOptionList("export").size() > 0) {
        return false;
    }

    return true;
}

TDEStartupLogo* showStartupLogo(TDEConfig* cfg, bool showGUI) {
    TDEStartupLogo* startLogo = 0L;

    cfg->setGroup( "General Options" );
    bool showLogo = cfg->readBoolEntry( "logo", true );
    if (showGUI && showLogo) {
#if KDE_IS_VERSION(3,3,90)
        startLogo = new TDEStartupLogo(0);
        startLogo->setHideEnabled(true);
        KWin::setMainWindow(startLogo, UMLApp::app()->winId());
#else
        startLogo = new TDEStartupLogo(UMLApp::app());
        startLogo->setHideEnabled(true);
#endif
        KWin::setState(startLogo->winId(), NET::KeepAbove);
        startLogo->show();
        TQApplication::flushX();
    }

    return startLogo;
}

void initDocument(TDECmdLineArgs *args, TDEConfig* cfg) {
    if ( args -> count() ) {
        UMLApp::app()->openDocumentFile( args->url( 0 ) );
    } else {
        cfg->setGroup( "General Options" );
        bool last = cfg->readBoolEntry( "loadlast", false );
        TQString file = cfg->readPathEntry( "lastFile" );
        if( last && !file.isEmpty() ) {
            UMLApp::app()->openDocumentFile( KURL( file ) );
        } else {
            UMLApp::app()->newDocument();
        }
    }
}

void exportAllViews(TDECmdLineArgs *args, const QCStringList &exportOpt) {
    TQString extension(exportOpt.last());
    kDebug() << "extension: " << extension << endl;

    // export to the specified directory, or the directory where the file is saved
    // if no directory was specified
    KURL directory;
    QCStringList directoryOpt = args->getOptionList("directory");
    if (directoryOpt.size() > 0) {
        directory = TDECmdLineArgs::makeURL(directoryOpt.last());
    } else {
        directory = KURL(UMLApp::app()->getDocument()->URL().directory());
    }

    bool useFolders = args->isSet("use-folders");

    kDebug() << "directory: " << directory.prettyURL() << endl;

    // the event is posted so when the QT loop begins it's processed. UMLApp process this event executing
    // the method it provides for exporting the views. Once all the views were exported, a quit event
    // is sent and the app finishes without user interaction
    kapp->postEvent(UMLApp::app(), new CmdLineExportAllViewsEvent(extension, directory, useFolders));
}