aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Android_Studio/settings.jar
blob: 3646d910be714c238eb0c3f906d9e8ab6a5d7ff6 (plain)
ofshex dumpascii
0000 50 4b 03 04 14 00 08 08 08 00 cf 64 89 4a 00 00 00 00 00 00 00 00 00 00 00 00 19 00 00 00 6f 70 PK.........d.J................op
0020 74 69 6f 6e 73 2f 63 6f 6c 6f 72 73 2e 73 63 68 65 6d 65 2e 78 6d 6c b3 49 2c 28 c8 c9 4c 4e 2c tions/colors.scheme.xml.I,(..LN,
0040 c9 cc cf b3 e3 52 50 b0 49 ce cf 2d c8 cf 4b cd 2b 51 c8 4b cc 4d b5 55 72 4d c9 2c c9 2f 72 ce .....RP.I..-..K.+Q.K.M.UrM.,./r.
0060 cf c9 2f 2a f6 4d cc 4b 4c 4f 2d f2 cc 2d c8 51 02 29 06 2a 4f cf c9 4f 4a cc 89 4f 06 c9 c7 17 ../*.M.KLO-..-.Q.).*O..OJ..O....
0080 27 67 a4 e6 a6 42 35 86 05 1b 19 18 9a ea ba 24 16 65 2b 29 e8 83 cd d6 87 1b 6e c7 65 a3 8f 6c 'g...B5........$.e+)......n.e..l
00a0 33 00 50 4b 07 08 a9 fc 39 38 6b 00 00 00 87 00 00 00 50 4b 03 04 0a 00 00 08 00 00 77 67 89 4a 3.PK....98k.......PK........wg.J
00c0 00 00 00 00 00 00 00 00 00 00 00 00 07 00 00 00 63 6f 6c 6f 72 73 2f 50 4b 03 04 14 00 08 08 08 ................colors/PK.......
00e0 00 52 66 89 4a 00 00 00 00 00 00 00 00 00 00 00 00 14 00 00 00 63 6f 6c 6f 72 73 2f 4f 62 73 69 .Rf.J................colors/Obsi
0100 64 69 61 6e 2e 69 63 6c 73 e5 5c 6d 6f ea 3a 12 fe be bf 02 f5 07 6c 21 ef 48 3d 57 32 89 09 de dian.icls.\mo.:.......l!.H=W2...
0120 26 31 72 9c c3 61 bf 44 21 84 bd ec 6d a1 02 ce 95 ee bf 5f 3b 81 90 86 97 82 9d 73 d2 7b b6 91 &1r..a.D!...m......_;......s.{..
0140 aa 16 65 9c c7 e3 99 67 66 ec 09 4f db f4 f7 ec 35 eb ac 92 d7 ec cb 03 9e 6d 97 f3 65 b2 7a e8 ..e....gf..O....5........m..e.z.
0160 fc 99 6d b6 cb f5 ea cb 43 4f 53 1e 3a 6f c9 26 5b ed e2 e2 de 2f 0f 4e b6 48 be bf ec 1e 7e fb ..m.....COS.:o.&[..../.N.H....~.
0180 47 a7 f3 b4 7e db b1 1b f7 03 78 28 80 71 38 06 36 0a 5c 36 48 f2 f2 9d 7d d8 fb 67 f7 a1 f3 78 G...~.....x(.q8.6.\6H...}..g...x
01a0 7a 33 74 10 c5 24 1e e2 80 c6 21 fa 37 3c 0a 28 1f de 1f 00 ff 78 bf bf 5e ad b7 6f 49 9a cd 0f z3t..$....!.7<.(.....x..^..oI...
01c0 72 e9 fa 65 bd d9 f2 3f eb 83 00 c7 81 4e cc 71 86 b1 8d 3d 4c ca 51 94 be 6e 28 87 27 d7 c5 6c r..e...?.....N.q...=L.Q..n(.'..l
01e0 40 20 ad 09 cc f2 9f ab 02 04 4f ea 4f 59 a8 7d 35 bd 24 84 83 10 7b 30 1e 00 fb d9 25 38 0a 9c @.........O.OY.}5.$...{0....%8..
0200 f8 19 4e 2b 08 d5 9e aa 5d 90 1d 22 0f 86 14 d0 28 8c f3 39 96 52 86 d2 37 75 fd 63 29 07 7a 90 ..N+....].."....(..9.R..7u.c).z.
0220 56 e5 52 7e 7d 2c 87 1c 08 e2 d3 71 e2 21 c1 7e fe 79 1c 4e 43 0a 7d e9 81 91 1b 60 52 01 68 69 V.R~},.....q.!.~.y.NC.}....`R.hi
0240 96 de bd 41 1d f5 71 7c 48 5c 86 6f 82 e8 28 1e 60 f6 8b a9 7d e8 21 9b 86 e5 d0 73 dd d4 45 20 ...A..q|H\.o..(.`...}.!....s..E.
0260 56 87 fe 31 a3 8e 09 1e 43 42 a7 12 c3 17 c3 95 62 7d cb 34 92 e4 06 31 ec a0 21 aa 1a 88 d5 37 V..1....CB......b}.4...1..!....7
0280 2f da 7f 45 30 c0 cc 73 46 20 e0 73 40 be cf dc 18 50 28 35 0a 81 76 44 42 f4 f5 fe 51 a2 e0 39 /..E0..sF..s@....P(5..vDB...Q..9
02a0 c0 93 e0 a8 b3 9e 61 ea 37 4c 3e 99 cf b3 39 fe be 63 fc 98 dd ed 59 e9 ef c9 ea 3f d9 cb 72 bb ......a.7L>...9..c....Y....?..r.
02c0 b3 d7 ab c5 cb 32 dd dd bd 66 af eb f9 72 b1 3c 03 e1 da bc dd 88 52 48 2a 6c 72 1b 93 a0 c0 81 .....2...f...r.<......RH*lr.....
02e0 8c 62 dd 88 d9 e1 d1 d9 ba 5a b7 db bd 20 91 d3 7e 10 f9 03 48 ea ac 6a f5 fa 46 ff 92 82 7d 48 .b.......Z......~...H..j..F...}H
0300 47 d8 89 43 38 06 04 30 6e 3f a1 e4 6b 64 79 b0 c7 b3 64 ae f5 f4 85 d1 bf 20 59 9a 4f 6c 03 cf G..C8..0n?..kdy...d.......Y.Ol..
0320 8b 01 a5 04 0d 22 0a 8f ae a4 9b 9a 7a 71 b6 04 b9 23 1a fb 80 b8 28 a8 3d 56 55 f8 75 41 2e 64 ....."......zq...#....(.=VU.uA.d
0340 c4 68 73 66 3c ab e0 ac 9b 29 d9 a5 25 29 45 29 04 24 d7 76 4d cb d7 56 a7 10 46 38 38 67 09 37 .hsf<....)..%)E).$.vM..V..F88g.7
0360 4a 0e 19 f3 d6 24 af 02 be 80 f3 ea 82 4e 46 88 ad 01 4b 1e aa 0b d1 e5 d7 21 ac 3f 1e e3 fa 53 J....$.......NF...K......!.?...S
0380 b2 db 6d 96 b3 ef bb ec 7c 98 1f 84 94 00 9b 51 86 07 c2 30 4f 17 aa eb 5c 88 30 a1 fc 31 87 ff ..m.....|......Q...0O...\.0..1..
03a0 4e fc ef 74 d2 86 69 a5 b3 5e 39 01 8e a9 32 c4 d3 63 21 7f 16 51 c0 18 0c e4 ca 6c 0c 4d d2 b5 N..t..i..^9...2..c!..Q.....l.M..
03c0 94 d9 5c 0c 0d 0e a6 3e 66 cc f2 69 14 34 00 0e 27 78 be 6a 90 88 03 a8 99 e5 3d 00 08 04 cf 63 ..\....>f..i.4..'x.j......=....c
03e0 8c 98 6b de ad 89 33 9e a5 26 9c 0c 84 80 bc ac d3 3f 3a e9 fa f5 95 65 dd e2 9a 30 e7 56 da 17 ..k...3..&.......?:....e...0.V..
0400 02 f0 69 6c a2 4c 88 bd 08 c6 38 a2 e3 88 8a c3 d0 d5 be 96 a5 32 30 ec 29 08 a4 61 18 fd 4a 60 ..il.L....8..........20.)..a..J`
0420 12 00 01 09 61 55 90 2c 8a 2c 35 8d 0a ef 0b e0 f0 81 cb a2 18 90 46 d2 b7 54 ad 6f c9 20 61 65 ....aU.,.,5...........F..T.o..ae
0440 81 0f 3c 79 95 d4 99 e3 06 27 af 25 52 02 e0 59 45 23 8d 7c b1 30 66 86 94 0a 8b 12 ad 01 20 e9 ..<y.....'.%R..YE#.|.0f.........
0460 bc 52 43 0b 00 89 42 96 b1 a2 40 0a 85 04 f1 45 21 65 55 2b 0a be 02 0f b1 c4 94 71 5f e0 c6 30 .RC...B...@....E!eU+.......q_..0
0480 b4 c1 b8 19 2a 14 8f 4e 7b 68 cf 70 3a c1 c4 e9 35 82 86 e5 62 a6 21 a3 a8 3d 1a e5 53 a1 51 3f ....*..N{h.p:...5...b.!..=..S.Q?
04a0 15 1a ed 73 a0 d9 27 e3 ac fc 16 49 6f 7e 84 93 f9 91 47 d1 27 c4 55 d4 b1 8d 60 91 a0 c3 02 cb ...s..'....Io~....G.'.U...`.....
04c0 9e 80 1a 61 1e f1 80 5f 60 f9 51 94 28 ae 24 07 0e 01 33 a2 23 8a 36 78 f9 00 62 40 f2 c2 55 1c ...a..._`.Q.(.$...3.#.6x..b@..U.
04e0 81 95 29 33 53 16 c1 33 a4 ad 62 38 56 0e 6d 94 0b ef 51 10 38 84 04 06 76 bb 50 18 b5 81 56 57 ..)3S..3..b8V.m...Q.8...v.P...VW
0500 84 25 56 14 04 4d e6 c5 77 63 70 b0 7d e0 f8 36 88 fd 0c 8c 98 02 b7 6d 8d f8 80 3c 47 e3 36 76 .%V..M..wcp.}..6.......m...<G.6v
0520 7f 8e 28 64 cc 42 da 34 d9 3a 20 3a 6d 33 72 0c a3 a0 d8 df e4 1b c1 6d d2 44 09 c4 81 8c bc 48 ..(d.B.4.:.:m3r........m.D.....H
0540 be 4f d8 e6 d2 b8 1e 1e b0 92 fa 2b 20 08 0c bc 56 03 2b e2 9b e5 7c a7 bf 95 4d c1 12 45 ce a2 .O.........+....V.+...|...M..E..
0560 36 8c 19 10 cf 69 d3 54 4a 24 c5 e9 49 bb 50 28 24 43 96 f7 b4 1e f1 cf 56 ec 6d da cb be fa 6b 6....i.TJ$..I.P($C......V.m....k
0580 b0 e2 ab cb 34 ba 1b 75 80 ed 81 01 94 20 42 79 bd 55 eb c1 36 73 05 0f db 9f 84 ff 98 9b 03 07 ....4..u......By.U..6s..........
05a0 50 89 24 52 3e 51 28 6a e2 36 23 35 ef 7a 28 4e cf 42 e4 b6 1a 1a f9 59 35 5b 93 76 c3 11 03 c1 P.$R>Q(j.6#5.z(N.B.....Y5[.v....
05c0 1c 64 d4 6a ad 37 26 90 fd c9 fc 95 d1 ee d4 1f 60 09 de 90 b7 d0 10 fa 88 9f 35 b7 6a 1a bc 59 .d.j.7&.........`.........5.j..Y
05e0 03 d9 ed a7 08 7b 1c b2 09 82 bc 99 16 e1 b8 8d 4d ab 03 02 b9 3a 4f da 22 28 f4 c7 1e a0 90 05 .....{..........M....:O."(......
0600 d6 c0 8d 80 7b 68 88 68 71 4d 1a 4d 94 64 88 bd 68 13 a4 f0 5b 33 1b bf 27 c9 cf 0d 09 93 8c 36 ....{h.hqM.M.d..h...[3..'......6
0620 19 fd d9 80 cf a0 dd 73 21 07 0d 87 65 33 a0 78 b7 82 a6 ab 5d 65 76 45 7b c5 b1 33 37 9b f1 49 .......s!...e3.x....]evE{..37..I
0640 f7 d1 42 57 34 31 37 e1 e0 0f 8d a7 e2 d8 2d 2d d1 12 31 ec 86 6e 64 a6 21 8c 9d 15 63 90 48 81 ..BW417.......--..1..nd.!...c.H.
0660 57 fa 9a a6 1a 62 e0 35 cd ec e9 62 de c7 c1 97 1d 9d c2 e0 55 4b d7 cd ae 20 78 d5 e8 5b 62 31 W....b.5...b........UK....x..[b1
0680 ff fd 3e 1e 67 b4 48 82 c3 24 92 8f 68 cc bc 8e d3 7b de ec cc 4c e1 eb cd 69 e1 19 6d ea 99 ae ..>.g.H..$..h....{...L...i..m...
06a0 aa 42 bd 21 90 25 e8 9f a1 5f 27 5f ee f0 7e 10 70 38 84 f6 49 7f 7d aa 2e d4 54 cc b8 3e 96 2d .B.!.%..._'_..~.p8..I.}...T..>.-
06c0 9e 48 a7 e3 63 0b a6 90 27 c1 6f d0 8e 78 61 d2 5c e7 98 32 37 ba 7d 21 3b 18 62 cf 69 32 a6 8a .H..c...'.o..xa.\..27.}!;.b.i2..
06e0 b4 c4 d4 3a 4a ef 43 ef e1 09 c3 3f 62 ab c2 db 47 9f 9b 49 0c 2c 73 c6 5b a6 3f b2 85 5a ab ea ...:J.C....?b...G..I.,s.[.?..Z..
0700 8d 52 ef 2c 48 c8 69 5c 18 40 c2 d2 f5 82 3e 0e fd 5d 24 9e 00 12 dc 9e 3d 9f 9d c4 42 eb 5b 3d .R.,H.i\.@....>..]$.....=...B.[=
0720 41 7e fe 58 b6 29 05 fc 7f 2d f7 71 3f 3b 66 b0 d9 ef e2 55 a1 26 88 c3 e8 f1 4b 30 11 aa cb de A~.X.)...-.q?;f....U.&....K0....
0740 3e 21 9f 47 41 c4 a6 50 6b 2a 2e 77 4a 5a 8e 4a 28 18 e2 86 62 52 92 25 99 25 e8 50 1f cb 36 15 >!.GA..Pk*.wJZ.J(...bR.%.%.P..6.
0760 93 50 f0 af e2 75 81 b2 ea 1c 12 e0 ca 6d a9 8a 57 28 ef 8f 4e 2e 2f 44 f9 56 c0 d5 b1 aa 27 0e .P...u.......m..W(..N./D.V....'.
0780 6d db d5 6a bb 4b 56 69 d6 79 db ac df b2 cd ee af ce 26 5b 64 9b 8c 7f 84 64 b6 fe df bf 91 75 m..j.KVi.y........&[d....d.....u
07a0 07 22 0f bb 0d f5 07 8b 37 9c e5 18 be 8d 11 6f 6c 65 16 47 24 0e 78 c5 b7 f1 39 88 7d fc 6c be ."......7......ole.G$.x...9.}.l.
07c0 2f f8 76 14 cb ed ee f1 35 79 eb ec d6 9d f5 ec bf 59 ba eb a4 eb d5 fe dd 5a 89 46 76 ab 3f 5f /.v.....5y.......Y.......Z.Fv.?_
07e0 5c 2b 19 cf e5 94 33 7e 5d 91 c9 5f ab bd 18 dd 6e a1 2a 5d 44 45 3e a0 f6 88 59 4a de 3e d5 50 \+....3~].._....n.*]DE>...YJ.>.P
0800 3f 99 65 88 ed 46 ec 5f 47 ab 1c c1 b7 4c 30 3e 33 9d 3f b2 bf c4 1f 6d bd cf c4 9b b0 93 c6 56 ?.e..F._G....L0>3.?....m.......V
0820 9e bf d0 19 85 9c 24 3c d8 58 f7 a7 d5 e5 d7 bd c9 df ad 52 f2 91 b9 d9 94 48 9c 99 42 08 88 3d ......$<.X.........R.....H..B..=
0840 8a 09 0c df 35 50 fe ed 72 cf ea 89 8b 5c 66 f1 ee cc a4 ed d5 d9 25 bb 65 da 74 5a 31 ef ce bb ....5P..r....\f.......%.e.tZ1...
0860 8b c5 5d 21 40 b9 d7 93 16 f9 cf cf 28 a3 ca 93 95 c3 19 7d cb 6b c6 f7 5c fe 2e ef 1e e5 fb 43 ..]!@.......(......}.k..\......C
0880 bf 0a 05 50 ec e0 f8 a4 15 5c e6 ad 5a 2b ed 2a d7 3a 6f ee f4 93 2b 15 a1 a9 a5 42 3b 65 fc e1 ...P.....\..Z+.*.:o...+....B;e..
08a0 95 f2 ba b1 5a 48 c2 fc a7 63 2c 53 58 5b 86 b9 50 cc 9f 11 82 a3 a0 f9 9c 73 de 53 34 75 26 84 ....ZH...c,SX[..P........s.S4u&.
08c0 66 b5 c9 b6 eb 97 3f b3 79 85 e5 93 34 cd b6 5b 71 38 b5 6f a6 b9 c9 79 6f 4d fc 1a c8 9c 84 dc f.....?.y...4..[q8.o...yoM......
08e0 fc 50 cd 35 41 54 ba a2 77 55 c1 03 43 2b 65 f4 f0 53 72 c5 09 41 2c bc fd 72 db 85 c5 b4 7e 95 .P.5AT..wU..C+e..Sr..A,..r....~.
0900 e8 33 21 98 19 65 f9 aa 89 c0 d9 d7 39 ff ad 9f 5e 5d 84 f3 f4 58 fd 1e 8c a7 c7 e2 2b ba 7e fb .3!..e......9...^]...X......+.~.
0920 1f 50 4b 07 08 00 bd ff 43 18 08 00 00 d7 4b 00 00 50 4b 03 04 14 00 08 08 08 00 52 66 89 4a 00 .PK.....C.....K..PK........Rf.J.
0940 00 00 00 00 00 00 00 00 00 00 00 15 00 00 00 63 6f 6c 6f 72 73 2f 4f 62 73 69 64 69 61 6e 32 2e ...............colors/Obsidian2.
0960 69 63 6c 73 ed 5c db 6e db 38 10 7d df af 08 f2 01 8d ad 3b 81 b4 00 2d d1 36 1b 49 f4 52 52 e3 icls.\.n.8.}.......;...-.6.I.RR.
0980 ec 8b 40 5d 8c 0d d0 26 81 e3 2e b0 7f bf a4 2c 5f e2 ca 8a 4d d1 76 0a 6c 05 14 4d e0 11 cf 0c ..@]...&.......,_...M.v.l..M....
09a0 87 33 67 66 e8 de be e6 7f 97 3f ca ab 27 f6 a3 fc 7c 4d b2 d7 c7 e2 91 3d 69 d7 57 ff 94 f3 d7 .3gf......?..'...|M.....=i.W....
09c0 c7 e7 a7 cf d7 7d 83 ff f4 c2 e6 e5 d3 22 5d 7e f8 f3 b5 57 ce d8 cf ef 8b eb 2f 7f 5c 5d dd 3e .....}......."]~...W....../.\].>
09e0 bf 2c f8 07 eb 37 f8 38 44 69 34 81 2e 0e 47 fc 25 ec fb 4f fe cb fe a7 de f5 d5 cd af 1f 46 1e .,...7.8Di4...G.%..O..........F.
0a00 8e 09 4d 87 24 8c d3 08 ff 85 36 02 da bb 9f 0f 61 b0 f9 7c f0 fc f4 fc fa c2 f2 b2 58 c9 e5 cf ..M.$.....6.....a..|........X...
0a20 df 9f e7 af e2 9f bb 2f 81 9e 87 bc 54 e0 8c 52 97 f8 84 ae df e2 98 99 e6 e4 f5 1b 76 c5 5c 48 ......./....T..R............v.\H
0a40 51 bc 23 00 4a 66 31 d6 2a 40 c9 fd 8e 90 ae e9 b9 3e db 27 44 c2 88 f8 28 1d 40 f7 6e 44 49 12 Q.#.Jf1.*@.......>.'D...(.@.nDI.
0a60 7a e9 1d 7a 58 cb 6a 19 97 36 f7 c8 8e 92 38 46 74 4b 74 b3 24 30 2c 03 ec
using FluentFTP;
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.ServiceModel;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.Core.DI;
using Tango.Core.Helpers;
using Tango.Core.IO;
using Tango.Logging;
using Tango.MachineStudio.Common.Authentication;
using Tango.MachineStudio.Common.Navigation;
using Tango.MachineStudio.Common.Notifications;
using Tango.MachineStudio.Common.StudioApplication;
using Tango.MachineStudio.Common.Update;
using Tango.SharedUI;
using Tango.MachineStudio.UI.Messages;

namespace Tango.MachineStudio.UI.ViewModels
{
    public enum UpdateStatus
    {
        None,
        CheckingForUpdate,
        UpToDate,
        UpdateAvailable,
        Downloading,
        Updating,
        UpdateCompleted,
        Error,
        RollingBack,
        RollbackCompleted,
        RollbackError,
    }

    public class UpdateViewVM : ViewModel
    {
        private String _appPath = AppDomain.CurrentDomain.BaseDirectory;
        private LogManager logManager = LogManager.Default;

        private INotificationProvider _notification;
        private INavigationManager _navigation;
        private IStudioApplicationManager _application;
        private IAuthenticationProvider _authentication;
        private CheckForUpdatesResponse _updateInfo;
        private TemporaryFolder _newPackageTempFolder;
        private TemporaryFolder _previousPackageTempFolder;

        private bool _forcedUpdate;
        public bool ForcedUpdate
        {
            get { return _forcedUpdate; }
            set { _forcedUpdate = value; RaisePropertyChangedAuto(); }
        }

        private UpdateStatus _status;
        public UpdateStatus Status
        {
            get { return _status; }
            set { _status = value; RaisePropertyChangedAuto(); }
        }

        private String _latestVersion;
        public String LatestVersion
        {
            get { return _latestVersion; }
            set { _latestVersion = value; RaisePropertyChangedAuto(); }
        }

        private String _updateComments;
        /// <summary>
        /// Gets or sets the latest version comments.
        /// </summary>
        public String UpdateComments
        {
            get { return _updateComments; }
            set { _updateComments = value; RaisePropertyChangedAuto(); }
        }

        private double _downloadProgress;

        public double DownloadProgress
        {
            get { return _downloadProgress; }
            set { _downloadProgress = value; RaisePropertyChangedAuto(); }
        }

        private double _rollbackProgress;

        public double RollbackProgress
        {
            get { return _rollbackProgress; }
            set { _rollbackProgress = value; RaisePropertyChangedAuto(); }
        }

        private double _updateProgress;

        public double UpdateProgress
        {
            get { return _updateProgress; }
            set { _updateProgress = value; RaisePropertyChangedAuto(); }
        }

        private String _currentUpdateFile;

        public String CurrentUpdateFile
        {
            get { return _currentUpdateFile; }
            set { _currentUpdateFile = value; RaisePropertyChanged(nameof(CurrentUpdateFile)); }
        }

        private bool _isRollbackAvailable;

        public bool IsRollbackAvailable
        {
            get { return _isRollbackAvailable; }
            set { _isRollbackAvailable = value; RaisePropertyChangedAuto(); }
        }

        public RelayCommand UpdateCommand { get; set; }

        public RelayCommand BackCommand { get; set; }

        public RelayCommand RestartCommand { get; set; }

        public RelayCommand TryAgainCommand { get; set; }

        public RelayCommand RollbackCommand { get; set; }

        public RelayCommand TryRollbackAgainCommand { get; set; }

        public UpdateViewVM(INotificationProvider notification, IAuthenticationProvider authentication, INavigationManager navigation, IStudioApplicationManager application)
        {
            _notification = notification;
            _navigation = navigation;
            _application = application;
            _authentication = authentication;

            LatestVersion = "1.0.0.2";
            Status = UpdateStatus.CheckingForUpdate;
            UpdateCommand = new RelayCommand(StartUpdate, () => Status == UpdateStatus.UpdateAvailable);
            BackCommand = new RelayCommand(BackToApplication, () => Status != UpdateStatus.Updating && !ForcedUpdate);
            RestartCommand = new RelayCommand(RestartApplication, () => Status == UpdateStatus.UpdateCompleted || Status == UpdateStatus.RollbackCompleted);
            TryAgainCommand = new RelayCommand(TryAgain, () => Status == UpdateStatus.Error);
            RollbackCommand = new RelayCommand(Rollback, () => Status != UpdateStatus.RollingBack && !ForcedUpdate);
            TryRollbackAgainCommand = new RelayCommand(TryRollbackAgain, () => Status == UpdateStatus.RollbackError);

            IsRollbackAvailable = File.Exists(GetRollbackFile());

            TangoMessenger.Default.Register<Messages.ForcedUpdateMessage>(HandleForcedUpdateMessage);
        }

        private void HandleForcedUpdateMessage(ForcedUpdateMessage msg)
        {
            ForcedUpdate = true;
            InvalidateRelayCommands();

            _updateInfo = msg.UpdateResponse;
            Status = UpdateStatus.UpdateAvailable;
            LatestVersion = _updateInfo.Version;
            UpdateComments = _updateInfo.Comments;
            StartUpdate();
        }

        public void OnNavigatedInto()
        {
            if (!ForcedUpdate)
            {
                CheckForUpdates();
            }
        }

        private void CheckForUpdates()
        {
            Status = UpdateStatus.CheckingForUpdate;

            ChannelFactory<IMachineStudioUpdateService> service = null;

            Task.Factory.StartNew(() =>
            {
                try
                {
                    Thread.Sleep(2000);

                    service = UpdateServiceHelper.GetUpdateServiceChannel();
                    var client = service.CreateChannel();

                    CheckForUpdatesResponse response = client.CheckForUpdates(new CheckForUpdatesRequest()
                    {
                        Email = _authentication.CurrentUser.Email,
                        Password = _authentication.CurrentUser.Password,
                        Version = _application.Version.ToString(),
                    });

                    if (response.IsUpdateAvailable)
                    {
                        _updateInfo = response;
                        Status = UpdateStatus.UpdateAvailable;
                        LatestVersion = response.Version;
                        UpdateComments = response.Comments;
                    }
                    else
                    {
                        Status = UpdateStatus.UpToDate;
                    }
                }
                catch (Exception ex)
                {
                    logManager.Log(ex, "Error while checking for version update!");
                    Status = UpdateStatus.Error;
                }
                finally
                {
                    if (service != null)
                    {
                        service.Close();
                    }
                }
            });
        }

        private void BackToApplication()
        {
            if (Status == UpdateStatus.Downloading)
            {
                if (!_notification.ShowQuestion("This will abort all update operations. Are you sure?"))
                {
                    return;
                }
            }

            _navigation.NavigateTo(NavigationView.MainView);
            Status = UpdateStatus.None;
        }

        private void StartUpdate()
        {
            DownloadProgress = 0;
            UpdateProgress = 0;

            Status = UpdateStatus.Downloading;

            Task.Factory.StartNew(() =>
            {
                var tempFile = TemporaryManager.CreateFile(".zip");

                try
                {
                    logManager.Log("Creating temporary file " + tempFile);

                    int fileSize = 0;

                    using (FileStreamWrapper fs = new FileStreamWrapper(tempFile.Path, FileMode.Create, (current) =>
                    {
                        InvokeUINow(() =>
                        {
                            Thread.Sleep(10);
                            DownloadProgress = ((double)current / (double)fileSize) * 100d;
                        });
                    }))
                    {
                        using (FtpClient ftp = new FtpClient(_updateInfo.FtpHost, _updateInfo.UserName, _updateInfo.Password))
                        {
                            logManager.Log("Connecting to FTP site: " + _updateInfo.FtpHost);
                            ftp.ConnectAsync().Wait();
                            logManager.Log("Retrieving download size...");
                            fileSize = (int)ftp.GetFileSize(_updateInfo.FilePath);
                            logManager.Log("Download size: " + fileSize + " bytes.");
                            logManager.Log("Starting download...");
                            ftp.DownloadAsync(fs, _updateInfo.FilePath).Wait();
                        }
                    }


                    Status = UpdateStatus.Updating;

                    _newPackageTempFolder = TemporaryManager.CreateFolder();
                    _newPackageTempFolder.Persist = true;

                    using (ZipFile zip = ZipFile.Read(tempFile.Path))
                    {
                        int currentEntry = 0;

                        zip.ExtractProgress += (x, args) =>
                        {
                            if (args.EventType == ZipProgressEventType.Extracting_AfterExtractEntry)
                            {
                                logManager.Log("Extracting " + Path.GetFileName(args.CurrentEntry.FileName));
                                UpdateProgress = ((double)(currentEntry++) / (double)zip.Entries.Count) * 100d;
                            }
                        };

                        foreach (ZipEntry entry in zip)
                        {
                            Thread.Sleep(10);

                            string newPath = Path.Combine(_newPackageTempFolder.Path, entry.FileName);

                            try
                            {
                                if (entry.IsDirectory)
                                {
                                    Directory.CreateDirectory(newPath);
                                }
                                else
                                {
                                    CurrentUpdateFile = Path.GetFileName(entry.FileName);
                                    entry.Extract(_newPackageTempFolder.Path, ExtractExistingFileAction.OverwriteSilently);
                                }
                            }
                            catch
                            {
                                logManager.Log("Could not extract file " + entry.FileName);
                            }
                        }
                    }

                    try
                    {
                        LogManager.Log("Backing up current version...");
                        CurrentUpdateFile = "Backing up current version...";

                        String rollbackFolder = GetRollbackFolder();
                        Directory.CreateDirectory(rollbackFolder);

                        String backFile = GetRollbackFile();

                        if (File.Exists(backFile))
                        {
                            File.Delete(backFile);
                        }

                        using (ZipFile backZip = new ZipFile(backFile))
                        {
                            int currentEntry = 0;

                            backZip.SaveProgress += (_, e) =>
                            {
                                UpdateProgress = ((double)(currentEntry++) / (double)backZip.Entries.Count) * 100d;
                            };

                            backZip.Password = "Aa123456";
                            backZip.AddDirectory(_appPath);
                            backZip.Save();
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.Log(ex, "Could not construct rollback.");
                        _notification.ShowWarning("Update center has failed to construct a rollback point for the current version. Version rollback will not be available.");
                    }

                    TangoIOC.Default.GetInstance<MainViewVM>().DisableCheckForUpdates = true;
                    Status = UpdateStatus.UpdateCompleted;
                }
                catch (Exception ex)
                {
                    logManager.Log(ex, "Error while extracting update package.");
                    Status = UpdateStatus.Error;
                }
                finally
                {
                    tempFile.Delete();
                }
            });
        }

        private void TryAgain()
        {
            CheckForUpdates();
        }

        private void RestartApplication()
        {
            try
            {
                Process p = new Process();

                if (Status == UpdateStatus.UpdateCompleted)
                {
                    p.StartInfo.FileName = _newPackageTempFolder + "\\Tango.MachineStudio.Updater.exe";
                }
                else if (Status == UpdateStatus.RollbackCompleted)
                {
                    p.StartInfo.FileName = _previousPackageTempFolder + "\\Tango.MachineStudio.Updater.exe";
                }

                p.StartInfo.UseShellExecute = true;
                p.StartInfo.Arguments = _appPath;
                p.Start();
            }
            catch (Exception ex)
            {
                if (ex.Message == "The operation was canceled by the user")
                {
                    _notification.ShowWarning("It seems like you refused to invoke our update utility. This prevents Machine Studio from completing the update process!");
                    return;
                }
            }
            Environment.Exit(0);
        }

        private void Rollback()
        {
            if (_notification.ShowQuestion("Are you sure you want to restore the previous version?"))
            {
                Status = UpdateStatus.RollingBack;

                try
                {
                    Task.Factory.StartNew(() =>
                    {
                        _previousPackageTempFolder = TemporaryManager.CreateFolder();
                        _previousPackageTempFolder.Persist = true;

                        using (ZipFile zip = new ZipFile(GetRollbackFile()))
                        {
                            zip.Password = "Aa123456";

                            int currentEntry = 0;

                            zip.ExtractProgress += (x, args) =>
                            {
                                if (args.EventType == ZipProgressEventType.Extracting_AfterExtractEntry)
                                {
                                    logManager.Log("Extracting " + Path.GetFileName(args.CurrentEntry.FileName));
                                    RollbackProgress = ((double)(currentEntry++) / (double)zip.Entries.Count) * 100d;
                                }
                            };

                            foreach (ZipEntry entry in zip)
                            {
                                Thread.Sleep(10);

                                string newPath = Path.Combine(_previousPackageTempFolder.Path, entry.FileName);

                                try
                                {
                                    if (entry.IsDirectory)
                                    {
                                        Directory.CreateDirectory(newPath);
                                    }
                                    else
                                    {
                                        entry.Extract(_previousPackageTempFolder.Path, ExtractExistingFileAction.OverwriteSilently);
                                    }
                                }
                                catch
                                {
                                    logManager.Log("Could not extract file " + entry.FileName);
                                }
                            }

                        }

                        File.Delete(GetRollbackFile());

                        Status = UpdateStatus.RollbackCompleted;
                    });
                }
                catch (Exception ex)
                {
                    Status = UpdateStatus.Error;
                    LogManager.Log(ex, "Error while trying to restore version.");
                    _notification.ShowError("An error occurred while trying to restore the previous version.");
                }
            }
        }

        private void TryRollbackAgain()
        {
            CheckForUpdates();
        }

        private String GetRollbackFolder()
        {
            String rollbackFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango", "Rollback");
            return rollbackFolder;
        }

        private String GetRollbackFile()
        {
            String backFile = Path.Combine(GetRollbackFolder(), Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName) + ".rollback");
            return backFile;
        }

        protected override void RaisePropertyChangedAuto([CallerMemberName] string caller = null)
        {
            base.RaisePropertyChangedAuto(caller);
            InvalidateRelayCommands();
        }
    }
}