File: Main.cpp

package info (click to toggle)
basic256 2.0.0.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 15,076 kB
  • sloc: cpp: 16,791; yacc: 3,979; lex: 1,446; makefile: 25
file content (192 lines) | stat: -rwxr-xr-x 6,722 bytes parent folder | download
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
/** Copyright (C) 2006, Ian Paul Larsen, Florin Oprea
 **
 **  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.
 **
 **  This program is distributed in the hope that it will be useful,
 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 **  GNU General Public License for more details.
 **
 **  You should have received a copy of the GNU General Public License along
 **  with this program; if not, write to the Free Software Foundation, Inc.,
 **  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 **/



#include <iostream>
#include <locale.h>
#if !defined(WIN32) || defined(__MINGW32__)
#include <unistd.h>
#endif


#include <QApplication>
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QFileInfo>
#include <QLibraryInfo>
#include <QLocale>
#include <QStatusBar>
#include <QtPlugin>
#include <QTranslator>
#include <QMetaType>

#include "Settings.h"
#include "Version.h"
#include "MainWindow.h"
#include "BasicEdit.h"


//Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);

extern MainWindow * mainwin;
extern BasicEdit * editwin;

#if defined(WIN32) && !defined(WIN32PORTABLE)
static void associateFileTypes(const QStringList &fileTypes)
{
    QString displayName = QGuiApplication::applicationDisplayName();
    QString filePath = QCoreApplication::applicationFilePath();
    QString fileName = QFileInfo(filePath).fileName();

    //Application
    QSettings s("HKEY_CURRENT_USER\\Software\\Classes\\Applications\\" + fileName, QSettings::NativeFormat);
    s.setValue("FriendlyAppName", displayName);
    s.beginGroup("SupportedTypes");
    foreach (const QString& fileType, fileTypes)
        s.setValue(fileType, QString());
    s.endGroup();
    s.beginGroup("shell");
    s.beginGroup("open");
    s.setValue("FriendlyAppName", displayName);
    s.beginGroup("Command");
    s.setValue(".", QChar('"') + QDir::toNativeSeparators(filePath) + QString("\" \"%1\""));


    //Associate .kbs files to BASIC-256 (Windows)
    QSettings ss("HKEY_CURRENT_USER\\Software\\Classes\\" , QSettings::NativeFormat);
    ss.beginGroup(".kbs");
    ss.setValue(".",fileName + QString(".kbs"));
    ss.endGroup();
    ss.beginGroup(fileName + QString(".kbs"));
    ss.beginGroup("shell");
    ss.beginGroup("open");
    ss.beginGroup("Command");
    ss.setValue(".", QChar('"') + QDir::toNativeSeparators(filePath) + QString("\" \"%1\""));
    ss.endGroup();
    ss.endGroup();
    ss.beginGroup("run");
    ss.setValue(".", QString("&Run"));
    ss.beginGroup("Command");
    ss.setValue(".", QChar('"') + QDir::toNativeSeparators(filePath) + QString("\" -r \"%1\""));
}
#endif




int main(int argc, char *argv[]) {
    QApplication qapp(argc, argv);
    qRegisterMetaType<std::vector<std::vector<double>>>("std::vector<std::vector<double>>");
    int guimode = 0;		// 0=normal, 1- r option, 2- app option
    QString localecode;		// either lang or the system localle - stored on mainwin for help display

    QCoreApplication::setOrganizationName(SETTINGSORG);
    QCoreApplication::setApplicationName(SETTINGSAPP);
    QCoreApplication::setApplicationVersion(VERSION);

#if defined(WIN32) && !defined(WIN32PORTABLE)
    associateFileTypes(QStringList(".kbs"));
#endif

    // Command Line Parser
    QCommandLineParser parser;
    parser.setApplicationDescription(QObject::tr("BASIC-256 is an easy to use version of BASIC designed to teach anybody (especially middle and high-school students) the basics of computer programming."));
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addPositionalArgument("file", QObject::tr("BASIC file in format <name.kbs>"));
    // Run option
    QCommandLineOption setRunOption(QStringList() << "r" << "run", QObject::tr("Run specified file"));
    parser.addOption(setRunOption);
    // Application option
    QCommandLineOption setAppOption(QStringList() << "a" << "app" << "application", QObject::tr("Run specified file as an application"));
    parser.addOption(setAppOption);
    // Language option
    QCommandLineOption setLanguageOption(QStringList() << "l" << "lang" << "language", QObject::tr("Set language to <language>."), QObject::tr("language"));
    parser.addOption(setLanguageOption);
    // Process the actual command line arguments given by the user
    parser.process(qapp);
    const QStringList args = parser.positionalArguments();

    // file is args.at(0)
    QString fileName;
    if(args.size()>0)
    if (!args.at(0).isEmpty())
        fileName=args.at(0);

    localecode = parser.value(setLanguageOption);
    if(localecode.isEmpty())
        localecode = QLocale::system().name();

    if (parser.isSet(setRunOption) and !fileName.isEmpty()) {
        guimode=1;
    }

    if (parser.isSet(setAppOption) and !fileName.isEmpty()) {
        guimode=2;
    }

    QTranslator qtTranslator;
#ifdef WIN32
    qtTranslator.load("qt_" + localecode);
#else
    qtTranslator.load("qt_" + localecode, QLibraryInfo::location(QLibraryInfo::TranslationsPath));
#endif
    qapp.installTranslator(&qtTranslator);

    QTranslator kbTranslator;
#ifdef WIN32
    kbTranslator.load("basic256_" + localecode, qApp->applicationDirPath() + "/Translations/");
#else
    bool ok;
    ok = kbTranslator.load("basic256_" + localecode, "/usr/share/basic256/");
    if (!ok) ok = kbTranslator.load("basic256_" + localecode, "/usr/local/share/basic256/");  // alternative location
#endif
    qapp.installTranslator(&kbTranslator);

    MainWindow mainwin(0, 0, localecode, guimode);
    mainwin.setObjectName( "mainwin" );
    mainwin.statusBar()->showMessage(QObject::tr("Ready."));
    mainwin.show();

    bool loaded=false;
 
#ifdef ANDROID
    // android - dont load initial file but set default folder to sdcard if exists
    if (QDir("/storage/sdcard0").exists()) {
        QDir::setCurrent("/storage/sdcard0");
    }
#else
    // load initial file and optionally start
    if (!fileName.isEmpty()) {
            QFileInfo fi(fileName);
        loaded = mainwin.loadFile(fi.absoluteFilePath());
        if(guimode!=0 && !loaded){
            return 0;
        }
        mainwin.ifGuiStateRun();
    }else if(guimode!=0){
        return 0;
    }
#endif

    setlocale(LC_ALL,"C");

    if(!loaded) mainwin.newProgram();
    return qapp.exec();
}