OSDN Git Service

5170b654b242ec9cb24b3f358641705ffab8efce
[wordring-tm/wordring-tm.git] / proxy / main.cpp
1 #include "mainwindow.h"
2 #include "tmeditorwidget.h"
3
4 #include "tmsocket.h"
5 #include "httpserver.h"
6 #include "tmhttp.h"
7 #include "tmservice.h"
8 #include "tmdatabase.h"
9
10 #include "settings.h"
11
12 #include <QApplication>
13 #include <QStandardPaths>
14
15 #include <QDesktopServices>
16 #include <QSettings>
17
18 #include <QLinkedList>
19 #include <QFont>
20 #include <QDir>
21 #include <QFile>
22
23 #include <windows.h>
24
25 #include "debug.h"
26
27 #define TM_VERSION 1
28 #define TM_DATABASE_VERSION 1
29
30 int main(int argc, char *argv[])
31 {
32         //SetDebugMemoryLeaks();
33
34         QApplication a(argc, argv);
35
36         a.setOrganizationName("wordring");
37         a.setApplicationName("tm");
38
39         QString path = QStandardPaths::writableLocation(
40                                         QStandardPaths::ConfigLocation) + "/settings.ini";
41         Settings settings(path, QSettings::IniFormat);
42         QString app_path = QCoreApplication::applicationDirPath();
43
44         bool debug = false;
45 #ifdef QT_DEBUG
46         debug = true;
47 #endif
48
49         bool remove = false;
50         remove = true; // 設定評価法が実装されるまで設定を常に消す。
51
52         // 設定ファイルのバージョンと違う場合、必要に応じてファイルや設定を消去する。
53         if(settings.value("Main/version", 0) != TM_VERSION)
54         {
55                 settings.setValue("Main/version", TM_VERSION);
56                 remove = true;
57         }
58         // DEBUG/RELEASEの違いで、必要に応じてファイルや設定を消去する。
59         if(debug != settings.value("Main/debug", false).toBool()) remove = true;
60         settings.setValue("Main/debug", debug);
61
62
63         // Language
64 #ifdef QT_DEBUG
65         QString mecab = QString(PROXYSRCDIR);
66         mecab += "/../third-party/lib/ipadic";
67         QDir dir(mecab);
68         settings.setValue("Japanese/mecabdic", dir.absolutePath());
69 #else
70         if(remove) settings.remove("Japanese/mecabdic");
71         if(!settings.contains("Japanese/mecabdic"))
72         {
73                 QString mecabdic = app_path + "\\ipadic";
74                 QDir dir(mecabdic);
75                 mecabdic = dir.absolutePath();
76                 settings.setValue("Japanese/mecabdic", mecabdic);
77         }
78
79 #endif
80
81         // HttpServer
82         if(remove) settings.remove("HttpServer/port");
83         if(!settings.contains("HttpServer/port"))
84                 settings.setValue("HttpServer/port", 8080);
85
86         // SocketServer
87         if(remove) settings.remove("SocketServer/port");
88         if(!settings.contains("SocketServer/port"))
89                 settings.setValue("SocketServer/port", 8081);
90
91         // ProxyModule
92         if(remove) settings.remove("ProxyModule/prefix");
93         if(!settings.contains("ProxyModule/prefix"))
94                 settings.setValue("ProxyModule/prefix", "/?u=");
95
96         // ProxyHandler
97 #ifdef QT_DEBUG
98                 settings.setValue("ProxyHandler/js_file", QString(PROXYSRCDIR) + "/tm-default.js");
99                 settings.setValue("ProxyHandler/css_file", QString(PROXYSRCDIR) + "/tm-default.css");
100 #else
101         if(remove) settings.remove("ProxyHandler/js_file");
102         if(!settings.contains("ProxyHandler/js_file"))
103                 settings.setValue("ProxyHandler/js_file", app_path + "/tm-default.js");
104         if(remove) settings.remove("ProxyHandler/css_file");
105         if(!settings.contains("ProxyHandler/css_file"))
106                 settings.setValue("ProxyHandler/css_file", app_path + "/tm-default.css");
107 #endif
108         // DefaultHtmlModule
109 #ifdef QT_DEBUG
110         settings.setValue("DefaultHtmlModule/file", QString(PROXYSRCDIR) + "/index.html");
111 #else
112         if(remove) settings.remove("DefaultHtmlModule/file");
113         if(!settings.contains("DefaultHtmlModule/file"))
114                 settings.setValue("DefaultHtmlModule/file", app_path + "/index.html");
115 #endif
116         // Database
117         if(remove) settings.remove("Database/root");
118         if(!settings.contains("Database/root"))
119         {
120                 QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
121                 path += "/";
122                 path += QString::number(TM_DATABASE_VERSION);
123                 settings.setValue("Database/root", path);
124         }
125 #ifdef QT_DEBUG
126         settings.setValue("WordringConnection/url", "ws://wordring.net/ws");
127 #else
128         if(remove) settings.remove("WordringConnection/url");
129         if(!settings.contains("WordringConnection/url"))
130                 settings.setValue("WordringConnection/url", "ws://wordring.net/ws");
131 #endif
132         TM::Service *service = new TM::Service(&settings, &a);
133
134         // メインウィンドウ。
135         MainWindow w(&settings, service);
136         w.setFont(QFont("Meiryo"));
137
138         service->load_languages(QApplication::applicationDirPath() + "/plugins");
139
140         HttpServer *server = new HttpServer(&settings, &w);
141         w.set_http_port(server->port());
142
143         TM::SocketServer *socket = new TM::SocketServer(&settings, service, &w);
144
145         server->install(new TM::DefaultHtmlModule(&settings, server->port(), server));
146         server->install(new TM::ProxyModule(
147                                                 &settings, server->port(), socket->port(), server));
148
149         QObject::connect(&w, SIGNAL(closing()), socket, SLOT(abort()));
150         w.show();
151         int result = a.exec();
152         delete service;
153
154         return result;
155 }