-
Notifications
You must be signed in to change notification settings - Fork 31
/
PersonalSettings.vue
574 lines (553 loc) · 16.4 KB
/
PersonalSettings.vue
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
<template>
<div id="google_prefs" class="section">
<h2>
<a class="icon icon-google-settings" />
{{ t('integration_google', 'Google data migration') }}
</h2>
<p v-if="!showOAuth" class="settings-hint">
{{ t('integration_google', 'No Google OAuth app configured. Ask your Nextcloud administrator to configure Google connected accounts admin section.') }}
</p>
<div v-else
id="google-content">
<h3>{{ t('integration_google', 'Authentication') }}</h3>
<button v-if="!connected" id="google-oauth" @click="onOAuthClick">
<span class="icon icon-external" />
{{ t('integration_google', 'Connect to Google') }}
</button>
<div v-else>
<div class="google-grid-form">
<label class="google-connected">
<a class="icon icon-checkmark-color" />
{{ t('integration_google', 'Connected as {user}', { user: state.user_name }) }}
</label>
<button id="google-rm-cred" @click="onLogoutClick">
<span class="icon icon-close" />
{{ t('integration_google', 'Disconnect from Google') }}
</button>
</div>
<br>
<div v-if="nbContacts > 0"
id="google-contacts">
<h3>{{ t('integration_google', 'Contacts') }}</h3>
<label>
<span class="icon icon-menu-sidebar" />
{{ t('integration_google', '{amount} Google contacts', { amount: nbContacts }) }}
</label>
<button id="google-import-contacts" @click="onImportContacts">
<span class="icon icon-contacts-dark" />
{{ t('integration_google', 'Import Google contacts in Nextcloud') }}
</button>
<select v-if="showAddressBooks"
v-model.number="selectedAddressBook">
<option :value="-1">
{{ t('integration_google', 'Choose where to import the contacts') }}
</option>
<option :value="0">
➕ {{ t('integration_google', 'New address book') }}
</option>
<option v-for="(ab, k) in addressbooks" :key="k" :value="k">
📕 {{ ab.name }}
</option>
</select>
<input v-if="showAddressBooks && selectedAddressBook === 0"
v-model="newAddressBookName"
type="text"
:placeholder="t('integration_google', 'address book name')">
<button v-if="showAddressBooks && selectedAddressBook > -1 && (selectedAddressBook > 0 || newAddressBookName)"
id="google-import-contacts-in-book"
:class="{ loading: importingContacts }"
@click="onFinalImportContacts">
<span class="icon icon-download" />
{{ t('integration_google', 'Import in {name} address book', { name: selectedAddressBookName }) }}
</button>
</div>
<br>
<div v-if="calendars.length > 0"
id="google-calendars">
<h3>{{ t('integration_google', 'Calendars') }}</h3>
<div v-for="cal in calendars" :key="cal.id" class="google-grid-form">
<label>
<AppNavigationIconBullet slot="icon" :color="getCalendarColor(cal)" />
{{ getCalendarLabel(cal) }}
</label>
<button
:class="{ loading: importingCalendar[cal.id] }"
@click="onCalendarImport(cal)">
<span class="icon icon-calendar-dark" />
{{ t('integration_google', 'Import calendar') }}
</button>
</div>
</div>
<br>
<div v-if="nbPhotos > 0"
id="google-photos">
<h3>{{ t('integration_google', 'Photos') }}</h3>
<label>
<span class="icon icon-toggle-pictures" />
{{ t('integration_google', '{amount} Google photos (>{formSize})', { amount: nbPhotos, formSize: humanFileSize(estimatedPhotoCollectionSize, true) }) }}
</label>
<button v-if="enoughSpace && !importingPhotos"
id="google-import-photos"
@click="onImportPhotos">
<span class="icon icon-picture" />
{{ t('integration_google', 'Import Google photos') }}
</button>
<span v-else-if="!enoughSpace">
{{ t('integration_google', 'Your Google photo collection size is estimated to be bigger than your remaining space left ({formSpace})', { formSpace: humanFileSize(freeSpace) }) }}
</span>
<div v-else>
<br>
{{ t('integration_google', '{amount} photos imported ({progress}%)', { amount: nbImportedPhotos, progress: photoImportProgress }) }}
<br>
{{ lastPhotoImportDate }}
<br>
<button @click="onCancelPhotoImport">
<span class="icon icon-close" />
{{ t('integration_google', 'Cancel photo import') }}
</button>
</div>
</div>
</div>
</div>
</div>
</template>
<script>
import { loadState } from '@nextcloud/initial-state'
import { generateUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import moment from '@nextcloud/moment'
import { showSuccess, showError } from '@nextcloud/dialogs'
import AppNavigationIconBullet from '@nextcloud/vue/dist/Components/AppNavigationIconBullet'
export default {
name: 'PersonalSettings',
components: {
AppNavigationIconBullet,
},
props: [],
data() {
return {
state: loadState('integration_google', 'user-config'),
calendars: [],
addressbooks: [],
nbContacts: 0,
nbPhotos: 0,
freeSpace: 0,
showAddressBooks: false,
selectedAddressBook: -1,
newAddressBookName: 'Google-contacts',
importingContacts: false,
importingCalendar: {},
importingPhotos: false,
lastPhotoImportTimestamp: 0,
nbImportedPhotos: 0,
photoImportLoop: null,
}
},
computed: {
showOAuth() {
return this.state.client_id && this.state.client_secret
},
connected() {
return this.state.token && this.state.token !== ''
&& this.state.user_name && this.state.user_name !== ''
},
selectedAddressBookName() {
return this.selectedAddressBook === 0
? this.newAddressBookName
: this.addressbooks[this.selectedAddressBook].name
},
selectedAddressBookUri() {
return this.selectedAddressBook === 0
? null
: this.addressbooks[this.selectedAddressBook].uri
},
estimatedPhotoCollectionSize() {
// we estimate with an average 1 MB size per photo
return this.nbPhotos * 1000000
},
enoughSpace() {
return this.nbPhotos === 0 || this.estimatedPhotoCollectionSize < this.freeSpace
},
lastPhotoImportDate() {
return this.lastPhotoImportTimestamp !== 0
? t('integration_google', 'Last photo import job at {date}', { date: moment.unix(this.lastPhotoImportTimestamp).format('LLL') })
: t('integration_google', 'Photo import process will begin soon')
},
photoImportProgress() {
return this.nbPhotos > 0 && this.nbImportedPhotos > 0
? parseInt(this.nbImportedPhotos / this.nbPhotos * 100)
: 0
},
},
watch: {
},
mounted() {
const paramString = window.location.search.substr(1)
// eslint-disable-next-line
const urlParams = new URLSearchParams(paramString)
const ghToken = urlParams.get('googleToken')
if (ghToken === 'success') {
showSuccess(t('integration_google', 'Successfully connected to Google!'))
} else if (ghToken === 'error') {
showError(t('integration_google', 'Google connection error:') + ' ' + urlParams.get('message'))
}
// get informations if we are connected
if (this.showOAuth && this.connected) {
this.getGoogleCalendarList()
this.getLocalAddressBooks()
this.getNbGoogleContacts()
this.getNbGooglePhotos()
this.getPhotoImportValues()
}
},
methods: {
onLogoutClick() {
this.state.token = ''
this.saveOptions()
},
saveOptions() {
const req = {
values: {
token: this.state.token,
},
}
const url = generateUrl('/apps/integration_google/config')
axios.put(url, req)
.then((response) => {
showSuccess(t('integration_google', 'Google options saved'))
if (response.data.user_name !== undefined) {
this.state.user_name = response.data.user_name
if (this.state.token && response.data.user_name === '') {
showError(t('integration_google', 'Incorrect access token'))
}
}
})
.catch((error) => {
showError(
t('integration_google', 'Failed to save Google options')
+ ': ' + error.response.request.responseText
)
})
.then(() => {
})
},
onOAuthClick() {
const redirectEndpoint = generateUrl('/apps/integration_google/oauth-redirect')
const redirectUri = window.location.protocol + '//' + window.location.host + redirectEndpoint
const oauthState = Math.random().toString(36).substring(3)
const scopes = [
'openid',
'profile',
'https://www.googleapis.com/auth/calendar.readonly',
'https://www.googleapis.com/auth/calendar.events.readonly',
'https://www.googleapis.com/auth/contacts.readonly',
'https://www.googleapis.com/auth/photoslibrary.readonly',
]
const requestUrl = 'https://accounts.google.com/o/oauth2/v2/auth?'
+ 'client_id=' + encodeURIComponent(this.state.client_id)
+ '&redirect_uri=' + encodeURIComponent(redirectUri)
+ '&response_type=code'
+ '&access_type=offline'
+ '&prompt=consent'
+ '&state=' + encodeURIComponent(oauthState)
+ '&scope=' + encodeURIComponent(scopes.join(' '))
const req = {
values: {
oauth_state: oauthState,
},
}
const url = generateUrl('/apps/integration_google/config')
axios.put(url, req)
.then((response) => {
window.location.replace(requestUrl)
})
.catch((error) => {
showError(
t('integration_google', 'Failed to save Google OAuth state')
+ ': ' + error.response.request.responseText
)
})
.then(() => {
})
},
getGoogleCalendarList() {
const url = generateUrl('/apps/integration_google/calendars')
axios.get(url)
.then((response) => {
if (response.data && response.data.length && response.data.length > 0) {
this.calendars = response.data
}
})
.catch((error) => {
showError(
t('integration_google', 'Failed to get calendar list')
+ ': ' + error.response.request.responseText
)
})
.then(() => {
})
},
getCalendarLabel(cal) {
return cal.summary || cal.id
},
getCalendarColor(cal) {
return cal.backgroundColor
? cal.backgroundColor.replace('#', '')
: '0082c9'
},
getPhotoImportValues() {
const url = generateUrl('/apps/integration_google/import-photos-info')
axios.get(url)
.then((response) => {
if (response.data && Object.keys(response.data).length > 0) {
this.lastPhotoImportTimestamp = response.data.last_import_timestamp
this.nbImportedPhotos = response.data.nb_imported_photos
this.importingPhotos = response.data.importing_photos
if (!this.importingPhotos) {
clearInterval(this.photoImportLoop)
}
}
})
.catch((error) => {
console.debug(error)
})
.then(() => {
})
},
getNbGooglePhotos() {
const url = generateUrl('/apps/integration_google/photo-number')
axios.get(url)
.then((response) => {
if (response.data && Object.keys(response.data).length > 0) {
this.nbPhotos = response.data.nbPhotos
this.freeSpace = response.data.freeSpace
}
})
.catch((error) => {
showError(
t('integration_google', 'Failed to get number of Google photos')
+ ': ' + error.response.request.responseText
)
})
.then(() => {
})
},
getNbGoogleContacts() {
const url = generateUrl('/apps/integration_google/contact-number')
axios.get(url)
.then((response) => {
if (response.data && Object.keys(response.data).length > 0) {
this.nbContacts = response.data.nbContacts
}
})
.catch((error) => {
showError(
t('integration_google', 'Failed to get number of Google contacts')
+ ': ' + error.response.request.responseText
)
})
.then(() => {
})
},
getLocalAddressBooks() {
const url = generateUrl('/apps/integration_google/local-addressbooks')
axios.get(url)
.then((response) => {
if (response.data && Object.keys(response.data).length > 0) {
this.addressbooks = response.data
}
})
.catch((error) => {
showError(
t('integration_google', 'Failed to get address book list')
+ ': ' + error.response.request.responseText
)
})
.then(() => {
})
},
onImportContacts() {
this.selectedAddressBook = -1
this.showAddressBooks = !this.showAddressBooks
},
onFinalImportContacts() {
this.importingContacts = true
const req = {
params: {
uri: this.selectedAddressBookUri,
key: this.selectedAddressBook,
newAddressBookName: this.selectedAddressBook > 0 ? null : this.newAddressBookName,
},
}
const url = generateUrl('/apps/integration_google/import-contacts')
axios.get(url, req)
.then((response) => {
const nbAdded = response.data.nbAdded
showSuccess(
this.n('integration_google', '{number} contact successfully imported in {name}', '{number} contacts successfully imported in {name}', nbAdded, { number: nbAdded, name: this.selectedAddressBookName })
)
this.showAddressBooks = false
})
.catch((error) => {
showError(
t('integration_google', 'Failed to get address book list')
+ ': ' + error.response.request.responseText
)
})
.then(() => {
this.importingContacts = false
})
},
onCalendarImport(cal) {
const calId = cal.id
this.$set(this.importingCalendar, calId, true)
const req = {
params: {
calId,
calName: this.getCalendarLabel(cal),
color: cal.backgroundColor || '#0082c9',
},
}
const url = generateUrl('/apps/integration_google/import-calendar')
axios.get(url, req)
.then((response) => {
const nbAdded = response.data.nbAdded
const calName = response.data.calName
showSuccess(
this.n('integration_google', '{number} event successfully imported in {name}', '{number} events successfully imported in {name}', nbAdded, { number: nbAdded, name: calName })
)
})
.catch((error) => {
showError(
t('integration_google', 'Failed to import Google calendar')
+ ': ' + error.response.request.responseText
)
})
.then(() => {
this.$set(this.importingCalendar, calId, false)
})
},
onImportPhotos() {
const req = {
params: {
path: null,
},
}
const url = generateUrl('/apps/integration_google/import-photos')
axios.get(url, req)
.then((response) => {
const targetPath = response.data.targetPath
showSuccess(
t('integration_google', 'Starting importing photos in {targetPath} directory', { targetPath })
)
this.getPhotoImportValues()
this.photoImportLoop = setInterval(() => this.getPhotoImportValues(), 10000)
})
.catch((error) => {
showError(
t('integration_google', 'Failed to start importing Google photos')
+ ': ' + error.response.request.responseText
)
})
.then(() => {
})
},
onCancelPhotoImport() {
this.importingPhotos = false
clearInterval(this.photoImportLoop)
const req = {
values: {
importing_photos: '0',
last_import_timestamp: '0',
nb_imported_photos: '0',
},
}
const url = generateUrl('/apps/integration_google/config')
axios.put(url, req)
.then((response) => {
})
.catch((error) => {
console.debug(error)
})
.then(() => {
})
},
humanFileSize(bytes, approx = false, si = false, dp = 1) {
const thresh = si ? 1000 : 1024
if (Math.abs(bytes) < thresh) {
return bytes + ' B'
}
const units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
let u = -1
const r = 10 ** dp
do {
bytes /= thresh
++u
} while (Math.round(Math.abs(bytes) * r) / r >= thresh && u < units.length - 1)
if (approx) {
return Math.floor(bytes) + ' ' + units[u]
} else {
return bytes.toFixed(dp) + ' ' + units[u]
}
},
},
}
</script>
<style scoped lang="scss">
.google-grid-form label {
line-height: 38px;
}
.google-grid-form input {
width: 100%;
}
.google-grid-form {
max-width: 600px;
display: grid;
grid-template: 1fr / 1fr 1fr;
button .icon {
margin-bottom: -1px;
}
}
#google_prefs .icon {
display: inline-block;
width: 32px;
}
#google_prefs .grid-form .icon {
margin-bottom: -3px;
}
.icon-google-settings {
background-image: url('./../../img/app-dark.svg');
background-size: 23px 23px;
height: 23px;
margin-bottom: -4px;
}
body.theme--dark .icon-google-settings {
background-image: url('./../../img/app.svg');
}
#google-content {
margin-left: 40px;
h3 {
font-weight: bold;
}
#google-photos > button,
#google-contacts > button {
width: 300px;
}
#google-photos > label,
#google-contacts > label {
width: 300px;
display: inline-block;
span {
margin-bottom: -2px;
}
}
}
::v-deep .app-navigation-entry__icon-bullet {
display: inline-block;
padding: 0;
height: 12px;
margin: 0 8px 0 10px;
}
</style>