1import QtQuick 2.0
2import AsemanTools 1.0
3import TelegramQmlLib 1.0
4import QtQuick.Controls 1.1
5import QtGraphicalEffects 1.0
6
7AsemanMain {
8    id: main
9    width: AsemanApp.readSetting("General/width", 1024)
10    height: AsemanApp.readSetting("General/height", 600)
11    color: "#00000000"
12    mainFrame: main_frame
13    focus: true
14    masterPalette.colorGroup: SystemPalette.Active
15    onVisibleChanged: refreshMask()
16
17    property variant authDialog
18    property variant tabFrame
19
20    property bool nativeTitleBar: false
21    property real shadowSize: nativeTitleBar? 32*Devices.density : 0
22    property real windowRadius: nativeTitleBar? 7*Devices.density : 0
23    property real titleBarHeight: nativeTitleBar? 24*Devices.density : 0
24
25    property alias profiles: profile_model
26    property alias webPageGrabber: web_grabber
27    property alias mapDownloader: map_downloader
28    property alias fontHandler: font_handler
29
30    property bool aboutMode: false
31    property bool dragging: false
32    property bool connectionAvailable: network.available
33
34    property color backColor0: "#eeeeee"
35    property color backColor1: "#cccccc"
36    property color backColor2: "#fafafa"
37    property color textColor0: "#111111"
38    property color textColor1: "#333333"
39    property color textColor2: "#888888"
40
41    property alias titleBarColor: tbar_cgrabber.color
42    property color masterColor: {
43        if(!Devices.isWindows8)
44            return masterPalette.highlight
45
46        var color = titleBarColor
47        var satur = Tools.colorSaturation(color)
48        if(satur < 0.2)
49            return masterPalette.highlight
50        else
51            return color
52    }
53    onMasterColorChanged: if(Devices.isWindows8) Cutegram.highlightColor = masterColor
54
55    onWidthChanged: {
56        refreshMask()
57        size_save_timer.restart()
58    }
59    onHeightChanged: {
60        refreshMask()
61        size_save_timer.restart()
62    }
63    onShadowSizeChanged: {
64        refreshMask()
65    }
66
67    onAboutModeChanged: {
68        if(aboutMode)
69            BackHandler.pushHandler(about, about.back)
70        else
71            BackHandler.removeHandler(about)
72    }
73
74    Keys.onEscapePressed: {
75        AsemanApp.back()
76    }
77
78    Keys.onPressed: {
79        if(event.modifiers & Qt.ControlModifier) {
80            switch(event.key) {
81            case Qt.Key_Q:
82                Cutegram.quit()
83                break
84
85            case Qt.Key_Tab:
86                tabFrame.nextDialog()
87                break
88
89            case Qt.Key_Backtab:
90                tabFrame.previousDialog()
91                break
92            }
93        }
94        else
95        if(event.modifiers & Qt.AltModifier) {
96            switch(event.key) {
97            case Qt.Key_Up:
98                tabFrame.previousDialog()
99                break
100
101            case Qt.Key_Down:
102                tabFrame.nextDialog()
103                break
104            }
105        }
106    }
107
108    FontHandler {
109        id: font_handler
110        onFontsChanged: if(!signalBlocker) AsemanApp.setSetting("General/fonts", save())
111        Component.onCompleted: {
112            signalBlocker = true
113            load(AsemanApp.readSetting("General/fonts"))
114            signalBlocker = false
115        }
116        property bool signalBlocker: false
117    }
118
119    TitleBarColorGrabber {
120        id: tbar_cgrabber
121        autoRefresh: Devices.isWindows8
122        Component.onCompleted: if(Devices.isWindows8) window = View
123    }
124
125    MapDownloaderQueue {
126        id: map_downloader
127        destination: Devices.localFilesPrePath + AsemanApp.homePath + "/maps"
128        size: Qt.size(320*Devices.density, 220*Devices.density)
129        mapProvider: MapDownloader.MapProviderGoogle
130        zoom: 15
131    }
132
133    WebPageGrabberQueue {
134        id: web_grabber
135    }
136
137    NetworkSleepManager {
138        id: network
139        host: Cutegram.defaultHostAddress
140        port: Cutegram.defaultHostPort
141        interval: 3000
142    }
143
144    Connections {
145        target: Cutegram
146        onBackRequest: AsemanApp.back()
147        onAboutAsemanRequest: qlist.currentIndex = 0
148    }
149
150    Connections {
151        target: AsemanApp
152        onBackRequest: {
153            var res = BackHandler.back()
154            if( !res ) {
155                if(Devices.isDesktop)
156                    tabFrame.showNull()
157                else
158                    Cutegram.close()
159            }
160        }
161    }
162
163    Connections {
164        target: View.window
165        onActiveChanged: {
166            if(Cutegram.closingState)
167                return
168
169            AsemanApp.setSetting("General/lastWindowState", View.active)
170        }
171    }
172
173    Timer {
174        id: size_save_timer
175        interval: 1000
176        onTriggered: {
177            AsemanApp.setSetting("General/width", width)
178            AsemanApp.setSetting("General/height", height)
179        }
180    }
181
182    ProfilesModel {
183        id: profile_model
184        configPath: AsemanApp.homePath
185    }
186
187    Timer {
188        id: init_timer
189        interval: 1500
190        Component.onCompleted: start()
191        onTriggered: {
192            if( profiles.count == 0 )
193                qlist.currentIndex = 3
194            else
195                qlist.currentIndex = 2
196        }
197    }
198
199    DropShadow {
200        id: drop_shadow
201        anchors.fill: shadow_scene
202        source: shadow_scene
203        horizontalOffset: 0
204        verticalOffset: View.window.active? hoveredOffset : 5*Devices.density
205        radius: View.window.active? shadowSize : shadowSize*0.4
206        samples: View.window.active? 32 : 16
207        visible: nativeTitleBar
208        color: "#80000000"
209
210        property real hoveredOffset: 10*Devices.density
211    }
212
213    Item {
214        id: shadow_scene
215        anchors.fill: parent
216
217        OpacityMask {
218            anchors.fill: main_scene
219            source: main_scene
220            maskSource: main_scene_mask
221            visible: nativeTitleBar
222        }
223
224        Rectangle {
225            id: main_scene_mask
226            anchors.fill: parent
227            color: "#ffffff"
228            radius: windowRadius
229            visible: false
230        }
231
232        Item {
233            id: main_scene
234            anchors.fill: parent
235            anchors.topMargin: nativeTitleBar? shadowSize*0.6 - drop_shadow.hoveredOffset : 0
236            anchors.bottomMargin: shadowSize
237            anchors.margins: nativeTitleBar? shadowSize*0.6 : 0
238            opacity: nativeTitleBar? 0 : 1
239
240            Item {
241                anchors.fill: parent
242                anchors.topMargin: titleBarHeight
243
244                AboutCutegram {
245                    id: about
246                    anchors.fill: parent
247
248                    function back() {
249                        aboutMode = false
250                    }
251
252                    MouseArea {
253                        anchors.fill: parent
254                        visible: !aboutMode
255                    }
256                }
257
258                Item {
259                    id: main_frame
260                    width: parent.width
261                    height: parent.height
262                    y: aboutMode? height : 0
263
264                    Behavior on y {
265                        NumberAnimation{ easing.type: Easing.OutCubic; duration: 400 }
266                    }
267
268                    QueueList {
269                        id: qlist
270                        anchors.fill: parent
271                        components: [aseman_about_component, splash_component, accounts_frame, auth_dlg_component]
272                        currentIndex: 1
273                        onCurrentIndexChanged: {
274                            prevIndex = tmpIndex
275                            tmpIndex = currentIndex
276                        }
277
278                        property int tmpIndex: 0
279                        property int prevIndex: 0
280                    }
281                }
282            }
283
284            Rectangle {
285                width: parent.width
286                height: titleBarHeight
287                visible: nativeTitleBar
288                color: View.window.active? Cutegram.currentTheme.dialogListBackground : Qt.lighter(Cutegram.currentTheme.dialogListBackground, 1.2)
289                border.width: 1*Devices.density
290                border.color: Qt.darker(color, 1.2)
291
292                Rectangle {
293                    width: parent.width-2*Devices.density
294                    anchors.horizontalCenter: parent.horizontalCenter
295                    anchors.bottom: parent.bottom
296                    color: parent.color
297                    height: 1*Devices.density
298                }
299
300                WindowDragArea {
301                    anchors.fill: parent
302                }
303            }
304
305            OSXTitleButtons {
306                height: titleBarHeight
307                width: 70*Devices.density
308                visible: nativeTitleBar
309                fullscreenButton: false
310            }
311
312            WindowResizeGrip {
313                anchors.right: parent.right
314                anchors.bottom: parent.bottom
315                visible: nativeTitleBar
316            }
317
318            MouseArea {
319                acceptedButtons: Qt.LeftButton | Qt.RightButton
320                anchors.fill: parent
321                visible: Devices.isMacX && Desktop.currentMenuObject
322                onClicked: if(Desktop.currentMenuObject) Desktop.currentMenuObject.hide()
323            }
324        }
325    }
326
327    Component {
328        id: auth_dlg_component
329        AuthenticateDialog {
330            anchors.fill: parent
331            onAccepted: {
332                var item = profiles.add(number)
333                item.name = "AA"
334                qlist.currentIndex = 2
335            }
336        }
337    }
338
339    Component {
340        id: accounts_frame
341        AccountsTabFrame {
342            id: tframe
343            anchors.fill: parent
344            property bool onceInstance: true
345            Component.onCompleted: tabFrame = tframe
346        }
347    }
348
349    Component {
350        id: splash_component
351        CutegramSplash {
352            id: splash
353            anchors.fill: parent
354        }
355    }
356
357    Component {
358        id: aseman_about_component
359        AsemanAbout {
360            id: aseman_about
361            anchors.fill: parent
362            Component.onCompleted: BackHandler.pushHandler(aseman_about, aseman_about.back)
363
364            function back() {
365                qlist.currentIndex = qlist.prevIndex
366            }
367        }
368    }
369
370    Component {
371        id: menubar_component
372        MenuBar {
373            Menu {
374                title: "About"
375                MenuItem { text: "About"; onTriggered: Cutegram.about() }
376                MenuItem { text: "Preferences"; onTriggered: Cutegram.configure() }
377            }
378        }
379    }
380
381    Component {
382        id: font_loader_component
383        FontLoader{}
384    }
385
386    function addAccount() {
387        qlist.currentIndex = 3
388        BackHandler.pushHandler(main, main.backToAccounts )
389    }
390
391    function backToAccounts() {
392        qlist.currentIndex = 2
393        BackHandler.removeHandler(main)
394    }
395
396    function installSticker(shortName) {
397        tabFrame.installSticker(shortName)
398    }
399
400    function refreshMask() {
401//        View.setMask(main_scene.x, main_scene.y, main_scene.width, main_scene.height)
402    }
403
404    Component.onCompleted: {
405        if(Devices.isMacX)
406            menubar_component.createObject(main)
407        if(Devices.isWindows) {
408            var fontsPath = AsemanApp.appPath + "/files/fonts/"
409            var fonts = Tools.filesOf(fontsPath)
410            for(var i=0; i<fonts.length; i++)
411                font_loader_component.createObject(main, {"source": Devices.localFilesPrePath + fontsPath + fonts[i]})
412        }
413
414        Desktop.menuStyle = Cutegram.currentTheme.menuStyleSheet
415        View.reverseScroll = (AsemanApp.readSetting("General/reverseScroll", 0) == "true")
416        nativeTitleBar = Cutegram.nativeTitleBar
417        refreshMask()
418    }
419}
420