summaryrefslogtreecommitdiff
path: root/Sparkles/BaseFetcher.cs
blob: 0cb1b90bbac911e4ff18c05d133f25cdc0a04213 (plain)
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
//   SparkleShare, a collaboration and sharing tool.
//   Copyright (C) 2010  Hylke Bons <hi@planetpeanut.uk>
//
//   This program is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser 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/>.


using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;

namespace Sparkles {

    public class SparkleFetcherInfo {
        public string Address; // TODO: Uri object
        public string RemotePath;

        public string Fingerprint;

        public string Backend;
        public string TargetDirectory;
        public bool FetchPriorHistory;

        public string AnnouncementsUrl; // TODO: Uri object
    }


    public abstract class BaseFetcher {

        public event Action Started = delegate { };
        public event Action Failed = delegate { };

        public event FinishedEventHandler Finished = delegate { };
        public delegate void FinishedEventHandler (StorageType storage_type, string [] warnings);

        public event ProgressChangedEventHandler ProgressChanged = delegate { };
        public delegate void ProgressChangedEventHandler (double percentage, double speed, string information);


        public abstract bool Fetch ();
        public abstract void Stop ();
        public bool IsActive { get; protected set; }
        public double ProgressPercentage { get; private set; }
        public double ProgressSpeed { get; private set; }


        protected abstract bool IsFetchedRepoEmpty { get; }
        public StorageType FetchedRepoStorageType { get; protected set; }
        public abstract bool IsFetchedRepoPasswordCorrect (string password);
        public abstract void EnableFetchedRepoCrypto (string password);

        public readonly List<StorageTypeInfo> AvailableStorageTypes = new List<StorageTypeInfo> ();


        public Uri RemoteUrl { get; protected set; }
        public string RequiredFingerprint { get; protected set; }
        public readonly bool FetchPriorHistory;
        public string TargetFolder { get; protected set; }
        public SparkleFetcherInfo OriginalFetcherInfo;


        protected List<string> warnings = new List<string> ();
        protected List<string> errors   = new List<string> ();

        public string [] Warnings {
            get {
                return warnings.ToArray ();
            }
        }

        public string [] Errors {
            get {
                return errors.ToArray ();
            }
        }

        

        protected BaseFetcher (SparkleFetcherInfo info)
        {
            FetchedRepoStorageType = StorageType.Unknown;

            AvailableStorageTypes.Add (
                new StorageTypeInfo (StorageType.Plain, "Plain Storage", "Nothing fancy;\nmaximum compatibility"));

            OriginalFetcherInfo = info;
            RequiredFingerprint = info.Fingerprint;
            FetchPriorHistory   = info.FetchPriorHistory;
            string remote_path  = info.RemotePath.Trim ("/".ToCharArray ());
            string address      = info.Address;

            if (address.EndsWith ("/", StringComparison.InvariantCulture))
                address = address.Substring (0, address.Length - 1);

            if (!remote_path.StartsWith ("/", StringComparison.InvariantCulture))
                remote_path = "/" + remote_path;

            if (!address.Contains ("://"))
                address = "ssh://" + address;

            TargetFolder = info.TargetDirectory;

            RemoteUrl = new Uri (address + remote_path);
            IsActive  = false;
        }


        Thread thread;

        public void Start ()
        {
            IsActive = true;
            Started ();

            Logger.LogInfo ("Fetcher", TargetFolder + " | Fetching folder: " + RemoteUrl);

            try {
                if (Directory.Exists (TargetFolder))
                    Directory.Delete (TargetFolder, recursive: true);
            
            } catch (IOException) {
                errors.Add ("\"" + TargetFolder + "\" is read-only.");
                Failed ();

                return;
            }

            thread = new Thread (() => {
                if (Fetch ()) {
                    Thread.Sleep (500);
                    Logger.LogInfo ("Fetcher", "Finished");

                    IsActive = false;
                    Finished (FetchedRepoStorageType, Warnings);

                } else {
                    Thread.Sleep (500);

                    if (IsActive) {
                        Logger.LogInfo ("Fetcher", "Failed");
                        Failed ();
                    
                    } else {
                        Logger.LogInfo ("Fetcher", "Failed: cancelled by user");
                    }

                    IsActive = false;
                }
            });

            thread.Start ();
        }


        public void Complete ()
        {
            if (FetchedRepoStorageType == StorageType.Unknown) {
                Complete (StorageType.Plain);
                return;
            }

            this.Complete (FetchedRepoStorageType);
        }


        public virtual string Complete (StorageType storage_type)
        {
            FetchedRepoStorageType = storage_type;

            if (IsFetchedRepoEmpty)
                CreateInitialChangeSet ();
            
            return Path.GetRandomFileName ().SHA256 ();
        }


        // Create an initial change set when the
        // user has fetched an empty remote folder
        void CreateInitialChangeSet ()
        {
			string n = Environment.NewLine;
            string file_path = Path.Combine (TargetFolder, "SparkleShare.txt");

            var uri_builder = new UriBuilder (RemoteUrl);

            // Don't expose possible username or password
            if (RemoteUrl.Scheme.StartsWith ("http", StringComparison.InvariantCultureIgnoreCase)) {
                uri_builder.UserName = "";
                uri_builder.Password = "";
            }

            string text = "Congratulations, you've successfully created a SparkleShare repository!" + n +
                n +
                "Any files you add or change in this folder will be automatically synced to " + n +
                uri_builder.Uri + " and everyone connected to it." + n +
                n +
                "SparkleShare is an Open Source software program that helps people collaborate and " + n +
                "share files. If you like what we do, consider buying us a beer: http://www.sparkleshare.org/" + n +
                n +
                "Have fun! :)" + n;

            if (FetchedRepoStorageType == StorageType.Encrypted)
                text = text.Replace ("a SparkleShare repository", "an encrypted SparkleShare repository");

            File.WriteAllText (file_path, text);
        }


        DateTime progress_last_change = DateTime.Now;

        protected void OnProgressChanged (double percentage, double speed, string information) {
            // Only trigger the ProgressChanged event once per second
            if (DateTime.Compare (this.progress_last_change, DateTime.Now.Subtract (new TimeSpan (0, 0, 0, 1))) >= 0)
                return;

            ProgressChanged (percentage, speed, information);
        }


        public static string GetBackend (string address)
        {
            if (address.StartsWith ("ssh+", StringComparison.InvariantCultureIgnoreCase)) {
                string backend = address.Substring (0, address.IndexOf ("://", StringComparison.InvariantCulture));
                backend = backend.Substring (4);

                return char.ToUpper (backend [0]) + backend.Substring (1);
            }

            return "Git";
        }


        public virtual string FormatName ()
        {
            return Path.GetFileName (RemoteUrl.AbsolutePath);
        }


        public void Dispose ()
        {
            if (thread != null)
                thread.Abort ();
        }


        protected string [] ExcludeRules = {
            "*.autosave", // Various autosaving apps
            "*~", // gedit and emacs
            ".~lock.*", // LibreOffice
            "*.part", "*.crdownload", // Firefox and Chromium temporary download files
            ".*.sw[a-z]", "*.un~", "*.swp", "*.swo", // vi(m)
            ".directory", // KDE
            "*.kate-swp", // Kate
            ".DS_Store", "Icon\r", "._*", ".Spotlight-V100", ".Trashes", // Mac OS X
            "*(Autosaved).graffle", // Omnigraffle
            "Thumbs.db", "Desktop.ini", // Windows
            "~*.tmp", "~*.TMP", "*~*.tmp", "*~*.TMP", // MS Office
            "~*.ppt", "~*.PPT", "~*.pptx", "~*.PPTX",
            "~*.xls", "~*.XLS", "~*.xlsx", "~*.XLSX",
            "~*.doc", "~*.DOC", "~*.docx", "~*.DOCX",
            "~$*",
            "*.a$v", // QuarkXPress
            "*/CVS/*", ".cvsignore", "*/.cvsignore", // CVS
            "/.svn/*", "*/.svn/*", // Subversion
            "/.hg/*", "*/.hg/*", "*/.hgignore", // Mercurial
            "/.bzr/*", "*/.bzr/*", "*/.bzrignore", // Bazaar
            "*<*", "*>*", "*:*", "*\"*", "*|*", "*\\?*", "*\\**", "*\\\\*" // Not allowed on Windows systems,
            // see (http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx)
        };
    }
}