2012-01-05 16:08:15 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2012-01-04 16:24:53 +01:00
|
|
|
#include "macfslistener.h"
|
|
|
|
|
|
|
|
#include <CoreFoundation/CFArray.h>
|
|
|
|
#include <Foundation/NSArray.h>
|
|
|
|
#include <Foundation/NSString.h>
|
|
|
|
|
|
|
|
#include "core/logging.h"
|
2012-01-04 16:33:12 +01:00
|
|
|
#include "core/scoped_nsobject.h"
|
2012-01-04 16:24:53 +01:00
|
|
|
|
2012-01-04 16:26:22 +01:00
|
|
|
MacFSListener::MacFSListener(QObject* parent)
|
2012-01-05 15:51:23 +01:00
|
|
|
: FileSystemWatcherInterface(parent),
|
2012-01-04 16:26:22 +01:00
|
|
|
run_loop_(NULL),
|
2012-01-04 16:24:53 +01:00
|
|
|
stream_(NULL) {
|
2012-03-06 12:53:20 +01:00
|
|
|
update_timer_.setSingleShot(true);
|
|
|
|
update_timer_.setInterval(2000);
|
|
|
|
connect(&update_timer_, SIGNAL(timeout()), SLOT(UpdateStream()));
|
2012-01-04 16:24:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MacFSListener::Init() {
|
|
|
|
run_loop_ = CFRunLoopGetCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacFSListener::EventStreamCallback(
|
|
|
|
ConstFSEventStreamRef stream,
|
|
|
|
void* user_data,
|
|
|
|
size_t num_events,
|
|
|
|
void* event_paths,
|
|
|
|
const FSEventStreamEventFlags event_flags[],
|
|
|
|
const FSEventStreamEventId event_ids[]) {
|
|
|
|
MacFSListener* me = reinterpret_cast<MacFSListener*>(user_data);
|
|
|
|
char** paths = reinterpret_cast<char**>(event_paths);
|
|
|
|
for (int i = 0; i < num_events; ++i) {
|
|
|
|
QString path = QString::fromUtf8(paths[i]);
|
|
|
|
qLog(Debug) << "Something changed at:" << path;
|
2012-01-05 17:11:39 +01:00
|
|
|
while (path.endsWith('/')) {
|
2012-01-05 16:56:46 +01:00
|
|
|
path.chop(1);
|
|
|
|
}
|
2012-01-04 16:24:53 +01:00
|
|
|
emit me->PathChanged(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacFSListener::AddPath(const QString& path) {
|
2012-01-05 15:51:23 +01:00
|
|
|
Q_ASSERT(run_loop_);
|
2012-01-04 16:24:53 +01:00
|
|
|
paths_.insert(path);
|
2012-03-06 12:53:20 +01:00
|
|
|
UpdateStreamAsync();
|
2012-01-04 16:24:53 +01:00
|
|
|
}
|
|
|
|
|
2012-01-29 18:39:28 +01:00
|
|
|
void MacFSListener::RemovePath(const QString& path) {
|
|
|
|
Q_ASSERT(run_loop_);
|
|
|
|
paths_.remove(path);
|
2012-03-06 12:53:20 +01:00
|
|
|
UpdateStreamAsync();
|
2012-01-29 18:39:28 +01:00
|
|
|
}
|
|
|
|
|
2012-01-05 16:01:43 +01:00
|
|
|
void MacFSListener::Clear() {
|
|
|
|
paths_.clear();
|
2012-03-06 12:53:20 +01:00
|
|
|
UpdateStreamAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacFSListener::UpdateStreamAsync() {
|
|
|
|
update_timer_.start();
|
2012-01-05 16:01:43 +01:00
|
|
|
}
|
|
|
|
|
2012-01-04 16:24:53 +01:00
|
|
|
void MacFSListener::UpdateStream() {
|
|
|
|
if (stream_) {
|
2012-01-05 15:51:23 +01:00
|
|
|
FSEventStreamStop(stream_);
|
2012-01-04 16:24:53 +01:00
|
|
|
FSEventStreamInvalidate(stream_);
|
2012-01-04 16:33:12 +01:00
|
|
|
FSEventStreamRelease(stream_);
|
2012-01-04 16:24:53 +01:00
|
|
|
stream_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paths_.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-04 16:33:12 +01:00
|
|
|
scoped_nsobject<NSMutableArray> array([[NSMutableArray alloc] init]);
|
2012-01-04 16:24:53 +01:00
|
|
|
|
|
|
|
foreach (const QString& path, paths_) {
|
2012-01-04 16:33:12 +01:00
|
|
|
scoped_nsobject<NSString> string(
|
|
|
|
[[NSString alloc] initWithUTF8String: path.toUtf8().constData()]);
|
|
|
|
[array addObject: string.get()];
|
2012-01-04 16:24:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FSEventStreamContext context;
|
|
|
|
memset(&context, 0, sizeof(context));
|
|
|
|
context.info = this;
|
|
|
|
CFAbsoluteTime latency = 1.0;
|
|
|
|
|
|
|
|
stream_ = FSEventStreamCreate(
|
|
|
|
NULL,
|
|
|
|
&EventStreamCallback,
|
|
|
|
&context, // Copied
|
2012-01-04 16:33:12 +01:00
|
|
|
reinterpret_cast<CFArrayRef>(array.get()),
|
2012-01-04 16:24:53 +01:00
|
|
|
kFSEventStreamEventIdSinceNow,
|
|
|
|
latency,
|
|
|
|
kFSEventStreamCreateFlagNone);
|
|
|
|
|
|
|
|
FSEventStreamScheduleWithRunLoop(stream_, run_loop_, kCFRunLoopDefaultMode);
|
|
|
|
FSEventStreamStart(stream_);
|
|
|
|
}
|