2011-06-14 22:14:03 +00:00
|
|
|
// 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;
|
2012-07-08 21:53:44 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Threading;
|
2011-06-14 22:14:03 +00:00
|
|
|
|
2012-07-08 21:53:44 +00:00
|
|
|
using IO = System.IO;
|
2011-06-14 22:14:03 +00:00
|
|
|
|
2012-07-08 21:53:44 +00:00
|
|
|
namespace SparkleLib {
|
2011-06-14 22:14:03 +00:00
|
|
|
|
2012-07-08 21:53:44 +00:00
|
|
|
public class SparkleWatcher : IO.FileSystemWatcher {
|
2011-06-14 22:14:03 +00:00
|
|
|
|
2012-07-08 21:53:44 +00:00
|
|
|
public List<SparkleRepoBase> ReposToNotify = new List<SparkleRepoBase> ();
|
2012-06-10 10:03:03 +00:00
|
|
|
|
2011-06-14 22:14:03 +00:00
|
|
|
|
2012-07-08 21:53:44 +00:00
|
|
|
public SparkleWatcher (SparkleRepoBase repo)
|
2011-06-14 22:14:03 +00:00
|
|
|
{
|
2012-07-08 21:53:44 +00:00
|
|
|
ReposToNotify.Add (repo);
|
|
|
|
|
|
|
|
Changed += Notify;
|
|
|
|
Created += Notify;
|
|
|
|
Deleted += Notify;
|
|
|
|
Renamed += Notify;
|
|
|
|
|
|
|
|
Filter = "*";
|
|
|
|
Path = IO.Path.GetDirectoryName (repo.LocalPath);
|
|
|
|
|
2011-06-14 22:14:03 +00:00
|
|
|
IncludeSubdirectories = true;
|
|
|
|
EnableRaisingEvents = true;
|
|
|
|
}
|
2012-06-10 10:03:03 +00:00
|
|
|
|
|
|
|
|
2012-07-08 21:53:44 +00:00
|
|
|
public void Notify (object sender, IO.FileSystemEventArgs args)
|
2012-06-10 10:03:03 +00:00
|
|
|
{
|
2012-07-08 21:53:44 +00:00
|
|
|
char separator = IO.Path.DirectorySeparatorChar;
|
|
|
|
string relative_path = args.FullPath.Substring (Path.Length);
|
|
|
|
relative_path = relative_path.Trim (new char [] {' ', separator});
|
2012-06-10 10:03:03 +00:00
|
|
|
|
2012-07-08 21:53:44 +00:00
|
|
|
// Ignore changes that happened in the parent path
|
|
|
|
if (!relative_path.Contains (separator.ToString ()))
|
|
|
|
return;
|
2012-06-10 10:03:03 +00:00
|
|
|
|
2012-07-08 21:53:44 +00:00
|
|
|
string repo_name = relative_path.Substring (0, relative_path.IndexOf (separator));
|
|
|
|
|
|
|
|
foreach (SparkleRepoBase repo in ReposToNotify) {
|
|
|
|
if (repo.Name.Equals (repo_name) && !repo.IsBuffering &&
|
|
|
|
(repo.Status != SyncStatus.SyncUp && repo.Status != SyncStatus.SyncDown)) {
|
|
|
|
|
|
|
|
Thread thread = new Thread (
|
|
|
|
new ThreadStart (delegate {
|
|
|
|
repo.OnFileActivity (args);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
thread.Start ();
|
|
|
|
}
|
|
|
|
}
|
2012-06-10 10:03:03 +00:00
|
|
|
}
|
2011-06-14 22:14:03 +00:00
|
|
|
}
|
|
|
|
}
|