Ubuntu Pastebin

Paste from mhall at Mon, 13 Apr 2015 12:37:21 +0000

Download as text
  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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.Components.ListItems 1.0 as ListItems
import "../components"
import "../models/QReddit"
import "../models/QReddit/QReddit.js" as QReddit

Page {
    id: frontpage

    property string subreddit: ""
    property string subredditFilter: "hot"

    property alias autoLoad: postsModel.autoLoad

    property alias lastPageAfter: postsModel.startAfter

    title: subreddit === "" ? "Frontpage" : "/r/"+subreddit

    state: "default"
    focus: true

    Keys.onPressed: {
        if (event.key == Qt.Key_F5) { refresh(); return; }
        if (event.key == Qt.Key_Home) { postsList.contentY = 0; return; }

        if (event.key == Qt.Key_PageDown) {
            var nextY = postsList.contentY + postsList.height - units.gu(10);
            if (nextY + postsList.height > postsList.contentHeight) {
                //nextY = postsList.contentHeight - postsList.height + moreLoaderItem.height;
            }
            postsList.contentY = nextY
            return;
        }
        if (event.key == Qt.Key_PageUp) {
            var prevY = postsList.contentY - postsList.height + units.gu(10);
            if (prevY < 0) {
                prevY = 0;
            }
            postsList.contentY = prevY
            return;
        }

        if ((event.modifiers & Qt.ControlModifier) && (event.key == Qt.Key_S)) {
            frontpage.state = "change_subreddit"
            head.sections.selectedIndex = 0
            return;
        }

        if ((event.modifiers & Qt.ControlModifier) && (event.key == Qt.Key_D)) {
            frontpage.state = "change_subreddit"
            head.sections.selectedIndex = 1
            return;
        }

        if ((event.modifiers & Qt.ControlModifier) && (event.key == Qt.Key_F)) {
            frontpage.state = "change_subreddit"
            head.sections.selectedIndex = 2
            subredditField.forceActiveFocus();
            subredditField.selectAll();
            return;
        }

    }
    PageHeadSections {
        id: defaultStateSections
        model: ["Hot", "New", "Top", "Controversial", "Rising"]
        selectedIndex: 0
        onSelectedIndexChanged: {
            subredditFilter = head.sections.model[head.sections.selectedIndex].toLowerCase()
        }
    }

    PageHeadSections {
        id: changeSubredditStateSections
        model: ["Subscriptions", "Defaults", "Search"]
        selectedIndex: uReadIt.qreddit.notifier.isLoggedIn ? 0 : 1
        property var subreddit_sources: ["subreddits_mine subscriber", "subreddits_default", "subreddits_search"]
        onSelectedIndexChanged: {
            subredditsList.subredditSource = subreddit_sources[selectedIndex]
        }
    }

    head.sections {
        model: defaultStateSections.model
        selectedIndex: defaultStateSections.selectedIndex
        onSelectedIndexChanged: {
            if (frontpage.state === "default") {
                defaultStateSections.selectedIndex = head.sections.selectedIndex
            } else if (frontpage.state === "change_subreddit") {
                changeSubredditStateSections.selectedIndex = head.sections.selectedIndex
            }
        }
    }

    onStateChanged: {
        if (state === "default") {
            frontpage.head.sections.model = defaultStateSections.model
            frontpage.head.sections.selectedIndex = defaultStateSections.selectedIndex
        } else if (state === "change_subreddit") {
            frontpage.head.sections.model = changeSubredditStateSections.model
            frontpage.head.sections.selectedIndex = changeSubredditStateSections.selectedIndex
        }

        if (state == "change_subreddit") {
            if (subredditsList.subredditSource === "" || !uReadIt.qreddit.notifier.isLoggedIn) {
                subredditsList.subredditSource = uReadIt.qreddit.notifier.isLoggedIn ? "subreddits_mine subscriber" : "subreddits_default"
            }

            subscriptionsPanel.open()
        } else {
            subscriptionsPanel.close()
        }
    }

    states: [
        PageHeadState {
            id: defaultPageState
            name: "default"

            head: frontpage.head

            contents: Label {
                LayoutMirroring.enabled: Qt.application.layoutDirection == Qt.RightToLeft
                visible: frontpage.state === "default"
                anchors {
                    left: parent.left
                    right: parent.right
                    verticalCenter: parent.verticalCenter
                }
                font.weight: Font.Light
                fontSize: "x-large"
                //color: styledItem.config.foregroundColor
                elide: Text.ElideRight

                text: frontpage.title
                MouseArea {
                    anchors.left: parent.left
                    width: parent.contentWidth
                    height: parent.height

                    onClicked: {
                        postsList.contentY = 0;
                    }
                }
            }

            backAction: Action {
                id: subredditAction
                objectName: "changeSubredditAction"

                iconName: "navigation-menu"
                //iconSource: Qt.resolvedUrl("../images/subreddit-menu.svg")
                text: i18n.tr("Subreddit")
                onTriggered: {
                    state = "change_subreddit"
                }

            }
            actions: [
                Action {
                    id: loginAction
                    text: "Login"
                    iconName: "contact"
                    visible: !uReadIt.qreddit.notifier.isLoggedIn
                    onTriggered: {
                        mainStack.push(Qt.resolvedUrl("UserAccountsPage.qml"));
                    }
                },
                Action {
                    id: inboxAction
                    text: "Inbox"
                    iconName: "email"
                    visible: uReadIt.qreddit.notifier.isLoggedIn && !inboxUnreadAction.visible
                    onTriggered: {
                        mainStack.push(Qt.resolvedUrl("UserMessagesPage.qml"), {'where': 'inbox'});
                    }
                },
                Action {
                    id: inboxUnreadAction
                    text: "New Unread"
                    iconSource: Qt.resolvedUrl("../images/email-unread.svg")
                    visible: uReadIt.qreddit.notifier.isLoggedIn && uReadIt.qreddit.notifier.hasUnreadMessages
                    onTriggered: {
                        mainStack.push(Qt.resolvedUrl("UserMessagesPage.qml"), {'where': 'unread'});
                    }
                },
                Action {
                    id: userAction
                    text: "Users"
                    iconName: "contact"
                    visible: uReadIt.qreddit.notifier.isLoggedIn
                    onTriggered: {
                        mainStack.push(Qt.resolvedUrl("UserAccountsPage.qml"));
                    }
                },
                Action {
                    id: settingsAction
                    text: "Settings"
                    iconName: "settings"
                    onTriggered: {
                        mainStack.push(Qt.resolvedUrl("SettingsPage.qml"));
                    }
                },
                Action {
                    id: logoutAction
                    text: "Logout"
                    iconName: "system-log-out"
                    visible: uReadIt.qreddit.notifier.isLoggedIn
                    onTriggered: {
                        var logoutConnObj = uReadIt.qreddit.logout();
                        logoutConnObj.onSuccess.connect(function() {
                            //postsModel.clear();
                            //postsModel.load();
                        });
                    }
                }
            ]
        },
        PageHeadState {
            id: changeState
            name: "change_subreddit"
            head: frontpage.head
//            sections {
//                model: ["My Subscriptions", "Defaults", "All"]
//                selectedIndex: 0
//                onSelectedIndexChanged: {
//                    console.log("Change subreddit list to: "+selectedIndex)
//                }
//            }
            backAction: Action {
                id: cancelChangeAction
                text: "back"
                iconName: "close"
                onTriggered: {
                    subredditField.text = subreddit
                    frontpage.state = "default"
                }
            }
            actions: [
                Action {
                    id: confirmChangeAction
                    text: "confirm"
                    iconName: "ok"
                    onTriggered: {
                        postsList.clear();
                        subreddit = subredditField.text
                        frontpage.state = "default"
                    }
                }
            ]
            contents: TextField {
                id: subredditField
                placeholderText: "Frontpage"
                width: parent.width - units.gu(2)
                text: subreddit
                visible : frontpage.state === "change_subreddit"
                inputMethodHints: Qt.ImhNoPredictiveText | Qt.ImhNoAutoUppercase
                onAccepted: confirmChangeAction.triggered(subredditField)
                onTextChanged: {
                    subredditsList.subredditSearch = text;
                }
                Keys.onPressed: { if (event.key == Qt.Key_Escape) frontpage.state = "default"; }
            }
        }
    ]

    Connections {
        target: uReadIt.qreddit.notifier

        onIsLoggedInChanged: {
            if (postsModel.loaded) {
                reload()
            }
            changeSubredditStateSections.selectedIndex = uReadIt.qreddit.notifier.isLoggedIn ? 2 : 0
        }

    }

    function reload() {
        postsList.clear();
        postsModel.clear();
        postsModel.load();
    }

    function refresh() {
        console.log("resetting postsModel")
        postsModel.startAfter = "";
        reload();
    }

    PullToRefresh {
        anchors.horizontalCenter: parent.horizontalCenter
        refreshing: postsList.isLoading
        onRefresh: frontpage.refresh()
        target: postsList
    }
    MultiColumnListView {
        id: postsList
        anchors.fill: parent
        anchors.margins: units.gu(1)
        width: parent.width

        columns: parent.width > units.gu(50) ? parent.width / units.gu(50) : 1

        rowSpacing: units.gu(1)
        colSpacing: units.gu(1)

        balanced: true

        Behavior on contentY {
                SmoothedAnimation { duration: 500 }
        }

        model: SubredditListModel {
            id: postsModel
            redditObj: uReadIt.qreddit
            subreddit: frontpage.subreddit
            filter: frontpage.subredditFilter
        }

        delegate: PostListItem {
            title: model.data.title
            thumbnail: (model.data.thumbnail != null && model.data.thumbnail != "self") ? model.data.thumbnail : ""
            author: "/r/"+model.data.subreddit
            domain: model.data.domain
            text: model.data.selftext
            score: model.data.score
            url: model.data.url
            comments: model.data.num_comments
            likes: model.data.likes
            property var postObj: new QReddit.PostObj(uReadIt.qreddit, model);

            onClicked: {
                if (model.data.is_self) {
                    mainStack.push(Qt.resolvedUrl("CommentsPage.qml"), {'postObj': model})
                } else {
                    mainStack.push(Qt.resolvedUrl("ArticlePage.qml"), {'postObj': model})
                }
            }
            onUpvoteClicked: {
                if (!uReadIt.qreddit.notifier.isLoggedIn) {
                    console.log("You can't vote when you're not logged in!");
                    return;
                }

                var voteConnObj = postObj.upvote();
                var postItem = this;
                voteConnObj.onSuccess.connect(function(response){
                    postItem.likes = model.data.likes = postObj.data.likes;
                    postItem.score = model.data.score = postObj.data.score;
                });
            }
            onDownvoteClicked: {
                if (!uReadIt.qreddit.notifier.isLoggedIn) {
                    console.log("You can't vote when you're not logged in!");
                    return;
                }
                var voteConnObj = postObj.downvote();
                var postItem = this;
                voteConnObj.onSuccess.connect(function(response){
                    postItem.likes = model.data.likes = postObj.data.likes;
                    postItem.score = model.data.score = postObj.data.score;
                });
            }
            onCommentsClicked: mainStack.push(Qt.resolvedUrl("CommentsPage.qml"), {'postObj': model})
        }

    }

    // TODO: refactor this
    Item {
        id: moreLoaderItem
        anchors.bottom: parent.bottom
        anchors.right: parent.right
        anchors.left: parent.left
        height: units.gu(2)

        property real loadMoreLength: units.gu(0)
        property real overflow: 0
        property variant spaceRect: null

        Connections {
            target: postsList

            onAtYEndChanged: {
                var pf = postsList
                if(pf.atYEnd && !pf.atYBeginning && (pf.contentHeight >= parent.height)) {
                    moreLoaderItem.overflow = pf.contentY - pf.contentHeight + pf.height
                    if ((moreLoaderItem.overflow > moreLoaderItem.loadMoreLength) && !moreLoaderItem.spaceRect) {
                        moreLoaderItem.spaceRect = Qt.createQmlObject("import QtQuick 2.0; Item{width: 1; height: " + moreLoaderItem.loadMoreLength + "}", frontpage)
                        postsModel.loadMore()
                    }

                } else {
                    moreLoaderItem.overflow = 0
                    moreLoaderItem.spaceRect = null
                }
            }
        }

    }


    Panel {
        id: subscriptionsPanel
        property int sectionIndex: 0
        anchors {
            left: parent.left
            bottom: parent.bottom
        }
        width: parent.width
        height: parent.height

        align: Qt.AlignLeft
        animate: true
        visible: opened || animating

        Rectangle {
            anchors.fill: parent
            color: Qt.rgba(uReadIt.theme.panelOverlay.r, uReadIt.theme.panelOverlay.g, uReadIt.theme.panelOverlay.b, 0.8)
            visible: !subscriptionsPanel.animating

            MouseArea {
                anchors.fill: parent
            }

        }

        Rectangle {
            width: units.gu(30)
            height: parent.height
            anchors.topMargin: units.gu(10) // Account for the page header

            color: uReadIt.theme.panelColor

            UbuntuListView {
                id: subredditsList
                anchors.fill: parent
                property string subredditSource: ""
                property string subredditSearch: subredditField.text

                header: ListItems.Standard {
                    text: subredditsList.subredditSource == "subreddits_search" ? subredditsList.subredditSearch : (subredditsList.subredditSource == "subreddits_mine subscriber" ? "Frontpage" : "All")
                    onClicked: {
                        postsList.clear();
                        subredditField.text = subredditsList.subredditSource == "subreddits_search" ? subredditsList.subredditSearch : (subredditsList.subredditSource == "subreddits_mine subscriber" ? "" : "All")
                        subreddit = subredditsList.subredditSource == "subreddits_search" ? subredditsList.subredditSearch : (subredditsList.subredditSource == "subreddits_mine subscriber" ? "" : "All")
                        frontpage.state = "default"
                    }
                }

                model: null
                onSubredditSourceChanged: {
                    subredditsList.model = null
                    if (subredditsList.subredditSource == "subreddits_search") {
                        var subsrConnObj = uReadIt.qreddit.getAPIConnection(subredditsList.subredditSource, {q: subredditsList.subredditSearch});
                        subsrConnObj.onConnection.connect(function(response){
                            subredditsList.model = response.data.children
                        });
                    } else {
                        var subsrConnObj = uReadIt.qreddit.getAPIConnection(subredditsList.subredditSource);
                        subsrConnObj.onConnection.connect(function(response){
                            subredditsList.model = response.data.children
                        });
                    }
                }
                onSubredditSearchChanged: {
                    if (subredditsList.subredditSource == "subreddits_search") {
                        var subsrConnObj = uReadIt.qreddit.getAPIConnection(subredditsList.subredditSource, {q: subredditsList.subredditSearch});
                        subsrConnObj.onConnection.connect(function(response){
                            subredditsList.model = response.data.children
                        });
                    }
                }

                delegate: ListItems.Standard {
                    text: modelData.data['display_name']
                    onClicked: {
                        postsList.clear();
                        subredditField.text = subredditsList.subredditSource == "subreddits_search" ? subredditsList.subredditSearch : modelData.data['display_name']
                        subreddit = modelData.data['display_name']
                        frontpage.state = "default"
                    }
                }
            }
            Scrollbar {
                flickableItem: subredditsList
                //align: Qt.AlignTrailing
            }
        }
    }

    flickable: uReadIt.height < units.gu(70) ? postsList : null
    clip: true

}
Download as text