forked from dicksonleong/Tweetian
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmain.qml
343 lines (295 loc) · 12.4 KB
/
main.qml
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
/*
Copyright (C) 2012 Dickson Leong
This file is part of Tweetian.
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 3 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, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.1
import Sailfish.Silica 1.0
import QtFeedback 5.0
import harbour.tweetian.Uploader 1.0
import "Services/Twitter.js" as Twitter
ApplicationWindow {
id: window
allowedOrientations: Orientation.All
initialPage: MainPage { id: mainPage }
cover: (settings.oauthToken != "" && settings.oauthTokenSecret != "") ? Qt.resolvedUrl("CoverPage.qml") : undefined;
property bool pendingTweet: false
Settings { id: settings }
Cache { id: cache }
Constant { id: constant }
ThemeEffect { id: basicHapticEffect; effect: ThemeEffect.Appear }
Rectangle {
id: infoBanner
width: mainPage.width
transformOrigin: Item.TopLeft
rotation: switch (window.orientation) {
case Orientation.Portrait: return 0;
case Orientation.Landscape: return 90;
case Orientation.PortraitInverted: return 180;
case Orientation.LandscapeInverted: return 270;
}
x: switch (window.orientation) {
case Orientation.Portrait: return 0;
case Orientation.Landscape: return mainPage.height;
case Orientation.PortraitInverted: return window.width;
case Orientation.LandscapeInverted: 0;
}
y: switch (window.orientation) {
case Orientation.Portrait: return 0;
case Orientation.Landscape: return 0;
case Orientation.PortraitInverted: return window.height;
case Orientation.LandscapeInverted: return mainPage.width;
}
height: infoText.height + 2 * Theme.paddingMedium
color: Theme.highlightBackgroundColor
opacity: 0.0
// On top of everything
z: 1
visible: opacity > 0.0
function showText(text) {
infoText.text = text
opacity = 0.9
console.log("INFO: " + text)
closeTimer.restart()
}
function showHttpError(errorCode, errorMessage) {
if (errorCode === 0) showText(qsTr("Server or connection error"))
else if (errorCode === 429) showText(qsTr("Rate limit reached, please try again later"))
else showText(qsTr("Error: %1").arg(errorMessage + " (" + errorCode + ")"))
}
Label {
id: infoText
anchors.top: parent.top
anchors.topMargin: Theme.paddingMedium
x: Theme.paddingMedium
width: parent.width - 2 * Theme.paddingMedium
color: Theme.highlightColor
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.Wrap
}
Behavior on opacity { FadeAnimation {} }
Timer {
id: closeTimer
interval: 3000
onTriggered: infoBanner.opacity = 0.0
}
MouseArea {
anchors.fill: parent
onClicked: {
closeTimer.stop()
infoBanner.opacity = 0.0
}
}
}
Item {
id: loadingRect
anchors.fill: parent
visible: false
z: 2
Rectangle {
anchors.fill: parent
color: "black"
opacity: 0.5
}
BusyIndicator {
visible: loadingRect.visible
running: visible
anchors.centerIn: parent
}
}
QtObject {
id: dialog
property Component __openLinkDialog: null
property Component __dynamicQueryDialog: null
property Component __messageDialog: null
property Component __tweetLongPressMenu: null
function createOpenLinkDialog(link, pocketCallback, instapaperCallback) {
infoBanner.showText(qsTr("Launching external web browser..."))
Qt.openUrlExternally(link)
}
function createQueryDialog(titleText, titleIcon, message, acceptCallback) {
if (!__dynamicQueryDialog) __dynamicQueryDialog = Qt.createComponent("Dialog/DynamicQueryDialog.qml")
var prop = { titleText: titleText, icon: titleIcon, message: message }
var dialog = __dynamicQueryDialog.createObject(pageStack.currentPage, prop)
dialog.accepted.connect(acceptCallback)
}
function createMessageDialog(titleText, message) {
if (!__messageDialog) __messageDialog = Qt.createComponent("Dialog/MessageDialog.qml")
__messageDialog.createObject(pageStack.currentPage, { titleText: titleText, message: message })
}
function createTweetLongPressMenu(model) {
if (!__tweetLongPressMenu) __tweetLongPressMenu = Qt.createComponent("Dialog/LongPressMenu.qml")
__tweetLongPressMenu.createObject(pageStack.currentPage, { model: model })
}
}
QtObject {
id: userPageHelper
/* This object is used by UserPage.qml by the RemorsePopup when follow/unfollow executes
We make this global to prevent error if user swipe away after follow/unfollow
*/
property bool isFollowing: false
function followOnSuccess(data, following) {
isFollowing = following;
if (isFollowing)
infoBanner.showText(qsTr("Followed the user %1 successfully").arg("@" + data.screen_name))
else
infoBanner.showText(qsTr("Unfollowed the user %1 successfully").arg("@" + data.screen_name))
}
function followOnFailure(status, statusText) {
infoBanner.showText(statusText)
}
}
QtObject {
id: tweetPageHelper
function deleteTweetOnSuccess(data) {
if (pageStack.currentPage.objectName == "tweetPage")
{
pageStack.pop()
}
infoBanner.showText(qsTr("Tweet deleted successfully"))
mainPage.timeline.removeTweet(data.id_str)
}
function commonOnFailure(status, statusText) {
infoBanner.showHttpError(status, statusText)
if (pageStack.currentPage.objectName == "tweetPage")
{
pageStack.pop()
}
}
}
ImageUploader {
id: imageUploader
service: settings.imageUploadService
networkAccessManager: QMLUtils.networkAccessManager()
onSuccess: {
if (service == ImageUploader.Twitter) internalTweet.postStatusOnSuccess(JSON.parse(replyData))
else {
var imageLink = ""
if (service == ImageUploader.TwitPic) imageLink = JSON.parse(replyData).url
else if (service == ImageUploader.MobyPicture) imageLink = JSON.parse(replyData).media.mediaurl
else if (service == ImageUploader.Imgly) imageLink = JSON.parse(replyData).url
Twitter.postStatus(internalTweet.tweetText+" "+imageLink, internalTweet.tweetId, internalTweet.latitude, internalTweet.longitude,
internalTweet.postStatusOnSuccess, internalTweet.commonOnFailure)
}
}
onFailure: internalTweet.commonOnFailure(status, statusText)
function run() {
imageUploader.setFile(internalTweet.tweetImage)
if (service == ImageUploader.Twitter) {
imageUploader.setParameter("status", internalTweet.tweetText)
if (internalTweet.tweetId) imageUploader.setParameter("in_reply_to_status_id", internalTweet.tweetId)
if (internalTweet.latitude != 0.0 && internalTweet.longitude != 0.0) {
imageUploader.setParameter("lat", internalTweet.latitude.toString())
imageUploader.setParameter("long", internalTweet.longitude.toString())
}
imageUploader.setAuthorizationHeader(Twitter.getTwitterImageUploadAuthHeader())
}
else {
if (service == ImageUploader.TwitPic) imageUploader.setParameter("key", constant.twitpicAPIKey)
else if (service == ImageUploader.MobyPicture) imageUploader.setParameter("key", constant.mobypictureAPIKey)
imageUploader.setParameter("message", internalTweet.tweetText)
imageUploader.setAuthorizationHeader(Twitter.getOAuthEchoAuthHeader())
}
imageUploader.send()
}
}
QtObject {
id: internalTweet
property bool exit: false
property string tweetType: "New"
property string screenName: ""
property string tweetText: ""
property string tweetImage: ""
property string tweetId: ""
property double latitude: 0.0
property double longitude: 0.0
/**
Extract a word from str at the specificed pos.
Example:
var text = "Hello world"
var word = getWordAt(text, n)
n = 0; word = ""
n = 1/2/3/4/5; word = "Hello"
n = 6; word = ""
n = 7/8/9/10/11; word = "world"
n > text.length; unexpected behaviour
*/
function getWordAt(str, pos) {
var left = str.slice(0, pos).search(/\S+$/)
if (left < 0) return ""
var right = str.slice(pos).search(/\s/)
if (right < 0) return str.slice(left)
return str.slice(left, right + pos)
}
function postStatusOnSuccess(data) {
switch (tweetType) {
case "New": infoBanner.showText(qsTr("Tweet sent successfully")); break;
case "Reply": infoBanner.showText(qsTr("Reply sent successfully")); break;
case "DM":infoBanner.showText(qsTr("Direct message sent successfully")); break;
case "RT": infoBanner.showText(qsTr("Retweet sent successfully")); break;
}
tweetId = ""
tweetType = ""
screenName = ""
tweetText = ""
tweetImage = ""
latitude = 0.0
longitude = 0.0
pendingTweet = false
/* Refresh timeline and DMs after succesfully sending tweet or DM */
mainPage.refreshAll();
}
function commonOnFailure(status, statusText) {
infoBanner.showHttpError(status, statusText)
pendingTweet = false
pageStack.push(Qt.resolvedUrl("NewTweetPage.qml"))
}
function postTweet(tweetid, tweettype, screenname, tweettext, tweetimage, lat, lon) {
tweetId = tweetid
tweetType = tweettype
screenName = screenname
tweetText = tweettext
tweetImage = tweetimage
latitude = lat
longitude = lon
pendingTweet = true
if (tweetType == "New" || tweetType == "Reply") {
if (tweetImage != '') {
imageUploader.run();
}
else /* if (tweettext.length()) */{
Twitter.postStatus(tweetText, tweetId ,latitude, longitude,
postStatusOnSuccess, commonOnFailure)
pendingTweet = true
}
/* FIXME
else if (tweetTextArea.errorHighlight && switchLong.checked) { // actually checked is pointless to check since we dont come here if not checked and message is to long
internal.postTweetLonger()
}
*/
}
else if (tweetType == "RT") {
console.log("id" + tweetId)
Twitter.postRetweet(tweetId, postStatusOnSuccess, commonOnFailure)
pendingTweet = true
}
else if (tweetType == "DM") {
Twitter.postDirectMsg(tweetText, screenName,
postStatusOnSuccess, commonOnFailure)
pendingTweet = true
}
}
}
Component.onCompleted: {
settings.loadSettings()
}
}