Skip to content

Commit

Permalink
OS X: implement MacClipboard.
Browse files Browse the repository at this point in the history
  • Loading branch information
rygwdn committed Jul 27, 2014
1 parent 3379e69 commit 09a227f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 14 deletions.
47 changes: 47 additions & 0 deletions src/platform/mac/macclipboard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright (c) 2014, Lukas Holecek <[email protected]>
This file is part of CopyQ.
CopyQ 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.
CopyQ 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 CopyQ. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef MACCLIPBOARD_H
#define MACCLIPBOARD_H

#include "platform/dummy/dummyclipboard.h"

#include <QClipboard>

class MacTimer;

class MacClipboard : public DummyClipboard {
Q_OBJECT
public:
explicit MacClipboard();

QVariantMap data(Mode mode, const QStringList &formats) const;

signals:
void changed(PlatformClipboard::Mode mode);

private:
long int m_prevChangeCount;
MacTimer *m_clipboardCheckTimer;

private slots:
virtual void clipboardTimeout();
};

#endif // MACCLIPBOARD_H
76 changes: 76 additions & 0 deletions src/platform/mac/macclipboard.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright (c) 2014, Lukas Holecek <[email protected]>
This file is part of CopyQ.
CopyQ 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.
CopyQ 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 CopyQ. If not, see <http://www.gnu.org/licenses/>.
*/

#include "macclipboard.h"

#include "common/common.h"
#include "common/mimetypes.h"

#include <QApplication>
#include <QMimeData>
#include <QClipboard>

#include "mactimer.h"

#include <Cocoa/Cocoa.h>
#include <Carbon/Carbon.h>

MacClipboard::MacClipboard():
m_prevChangeCount(0)
, m_clipboardCheckTimer(new MacTimer(this)) {

m_clipboardCheckTimer->setInterval(250);
m_clipboardCheckTimer->setTolerance(500);

connect(m_clipboardCheckTimer, SIGNAL(timeout()), this, SLOT(clipboardTimeout()));

m_clipboardCheckTimer->start();
}

QVariantMap MacClipboard::data(Mode mode, const QStringList &formats) const {
Q_UNUSED(mode);

// On OS X, when you copy files in Finder, etc. you get:
// - The file name(s) (not paths) as plain text
// - The file URI(s)
// - The icon (not thumbnail) for the type of item you have in various image formants
// We really only want the URI list, so throw the rest away

QStringList macFormats = QStringList(formats); // Copy so we can modify
if (mode == PlatformClipboard::Clipboard) {
const QMimeData *data = clipboardData(QClipboard::Clipboard);

if (data && data->formats().contains(mimeUriList) && macFormats.contains(mimeUriList)) {
macFormats = QStringList() << mimeUriList;
}
}

return DummyClipboard::data(mode, macFormats);
}


void MacClipboard::clipboardTimeout() {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSInteger newCount = [pasteboard changeCount];

if (newCount != m_prevChangeCount) {
m_prevChangeCount = newCount;
emit changed(PlatformClipboard::Clipboard);
}
}
5 changes: 0 additions & 5 deletions src/platform/mac/macplatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ class MacPlatform : public PlatformNativeInterface
void loadSettings() {}

PlatformClipboardPtr clipboard();

/**
* Get the number of changes to the clipboard (NSPasteboard::changeCount).
*/
long int getChangeCount();
};

#endif // MACPLATFORM_H
11 changes: 2 additions & 9 deletions src/platform/mac/macplatform.mm
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "macplatformwindow.h"
#include "platform/mac/macactivity.h"
#include "urlpasteboardmime.h"
#include "platform/dummy/dummyclipboard.h"
#include "macclipboard.h"

#include <QApplication>
#include <QCoreApplication>
Expand Down Expand Up @@ -172,7 +172,7 @@ PlatformPtr createPlatformNativeInterface()

PlatformClipboardPtr MacPlatform::clipboard()
{
return PlatformClipboardPtr(new DummyClipboard());
return PlatformClipboardPtr(new MacClipboard());
}

PlatformWindowPtr MacPlatform::getCurrentWindow()
Expand All @@ -185,13 +185,6 @@ PlatformPtr createPlatformNativeInterface()
return PlatformWindowPtr(new MacPlatformWindow(winId));
}

long int MacPlatform::getChangeCount()
{
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSInteger changeCount = [pasteboard changeCount];
return changeCount;
}

bool MacPlatform::isAutostartEnabled()
{
// Note that this will need to be done differently if CopyQ goes into
Expand Down
3 changes: 3 additions & 0 deletions src/platform/mac/macplatform.pri
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ SOURCES += \
../qxt/qxtglobalshortcut_mac.cpp \
platform/dummy/dummyclipboard.cpp


HEADERS += \
$$PWD/mactimer.h \
$$PWD/macclipboard.h \
$$PWD/foregroundbackgroundfilter.h \
platform/dummy/dummyclipboard.h

OBJECTIVE_SOURCES += \
$$PWD/macplatform.mm \
$$PWD/macplatformwindow.mm \
$$PWD/macactivity.mm \
$$PWD/macclipboard.mm \
$$PWD/copyqpasteboardmime.mm \
$$PWD/urlpasteboardmime.mm \
$$PWD/foregroundbackgroundfilter.mm \
Expand Down

0 comments on commit 09a227f

Please sign in to comment.