summaryrefslogtreecommitdiff
path: root/SparkleShare/SparkleEventLogController.cs
blob: 457733ec23ef83e0436f9238eb7dc2344c0b8835 (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
//   SparkleShare, a collaboration and sharing tool.
//   Copyright (C) 2010  Hylke Bons <hylkebons@gmail.com>
//
//   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/>.


using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;

using SparkleLib;

namespace SparkleShare {

    public class SparkleEventLogController {

        public event UpdateContentEventEventHandler UpdateContentEvent;
        public delegate void UpdateContentEventEventHandler (string html);

        public event UpdateChooserEventHandler UpdateChooserEvent;
        public delegate void UpdateChooserEventHandler (string [] folders);

        public event UpdateSizeInfoEventHandler UpdateSizeInfoEvent;
        public delegate void UpdateSizeInfoEventHandler (string size, string history_size);

        public event ContentLoadingEventHandler ContentLoadingEvent;
        public delegate void ContentLoadingEventHandler ();

        private string selected_folder;


        public string SelectedFolder {
            get {
                return this.selected_folder;
            }

            set {
                this.selected_folder = value;

                if (ContentLoadingEvent != null)
                    ContentLoadingEvent ();

                if (UpdateSizeInfoEvent != null)
                    UpdateSizeInfoEvent ("…", "…");

                Stopwatch watch = new Stopwatch ();
                watch.Start ();

                Thread thread = new Thread (new ThreadStart (delegate {
                    string html = HTML;
                    watch.Stop ();

                    // A short delay is less annoying than
                    // a flashing window
                    if (watch.ElapsedMilliseconds < 500)
                        Thread.Sleep (500 - (int) watch.ElapsedMilliseconds);

                    if (UpdateContentEvent != null)
                        UpdateContentEvent (html);
    
                    if (UpdateSizeInfoEvent != null)
                        UpdateSizeInfoEvent (Size, HistorySize);
                }));

                thread.Start ();
            }
        }

        public string HTML {
            get {
                List<SparkleChangeSet> change_sets = Program.Controller.GetLog (this.selected_folder);

                string html = Program.Controller.GetHTMLLog (change_sets);

                if (UpdateSizeInfoEvent != null)
                    UpdateSizeInfoEvent (Size, HistorySize);

                return html;
            }
        }

        public string [] Folders {
            get {
                return Program.Controller.Folders.ToArray ();
            }
        }

        public string Size {
            get {
                double size = 0;

                foreach (SparkleRepoBase repo in Program.Controller.Repositories) {
                    if (this.selected_folder == null)
                        size += repo.Size;
                    else if (this.selected_folder.Equals (repo.Name)) {
                        if (repo.Size == 0)
                            return "???";
                        else
                            return Program.Controller.FormatSize (repo.Size);
                    }
                }

                if (size == 0)
                    return "???";
                else
                    return Program.Controller.FormatSize (size);
            }
        }

        public string HistorySize {
            get {
                double size = 0;

                foreach (SparkleRepoBase repo in Program.Controller.Repositories) {
                    if (this.selected_folder == null)
                        size += repo.HistorySize;
                    else if (this.selected_folder.Equals (repo.Name)) {
                        if (repo.HistorySize == 0)
                            return "???";
                        else
                            return Program.Controller.FormatSize (repo.HistorySize);
                    }
                }

                if (size == 0)
                    return "???";
                else
                    return Program.Controller.FormatSize (size);
            }
        }


        public SparkleEventLogController ()
        {
            Program.Controller.AvatarFetched += delegate {
                if (UpdateContentEvent != null)
                    UpdateContentEvent (HTML);
            };

            Program.Controller.OnIdle += delegate {
                if (UpdateContentEvent != null)
                    UpdateContentEvent (HTML);

                if (UpdateSizeInfoEvent != null)
                    UpdateSizeInfoEvent (Size, HistorySize);
            };

            Program.Controller.FolderListChanged += delegate {
                if (this.selected_folder != null &&
                    !Program.Controller.Folders.Contains (this.selected_folder)) {

                    this.selected_folder = null;
                }

                if (UpdateChooserEvent != null)
                    UpdateChooserEvent (Folders);

                if (UpdateContentEvent != null)
                    UpdateContentEvent (HTML);

                if (UpdateSizeInfoEvent != null)
                    UpdateSizeInfoEvent (Size, HistorySize);
            };
        }


        public static void LinkClicked (string url)
        {
            if (url.StartsWith (Path.VolumeSeparatorChar.ToString ())) {
                Program.Controller.OpenFile (url);

            } else {
                Regex regex = new Regex (@"(.+)~(.+)~(.+)");
                Match match = regex.Match (url);

                if (match.Success) {
                    string folder_name = match.Groups [1].Value;
                    string revision    = match.Groups [2].Value;
                    string note        = match.Groups [3].Value;

                    Thread thread = new Thread (new ThreadStart (delegate {
                        Program.Controller.AddNoteToFolder (folder_name, revision, note);
                    }));

                    thread.Start ();
                }
            }
        }
    }
}