Skip to content

Commit

Permalink
Command option: copy item to tab; ignore same clipboard data; bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Mar 24, 2012
1 parent 4c901de commit ba2d2d6
Show file tree
Hide file tree
Showing 24 changed files with 465 additions and 233 deletions.
12 changes: 12 additions & 0 deletions src/client_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ QString serverName(const QString &name)
return name + sessionID;
}

uint hash(const QMimeData &data, const QStringList &formats)
{
QByteArray bytes;
foreach( QString mime, formats ) {
bytes = data.data(mime);
if ( !bytes.isEmpty() )
return qHash(bytes);
}

return 0;
}

QMimeData *cloneData(const QMimeData &data, const QStringList *formats)
{
QMimeData *newdata = new QMimeData;
Expand Down
1 change: 1 addition & 0 deletions src/client_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ bool readMessage(QIODevice *socket, QByteArray *msg);
void writeMessage(QIODevice *socket, const QByteArray &msg);
QLocalServer *newServer(const QString &name, QObject *parent=NULL);
QString serverName(const QString &name);
uint hash(const QMimeData &data, const QStringList &formats);
QMimeData *cloneData(const QMimeData &data, const QStringList *formats=NULL);
void raiseWindow(WId wid);

Expand Down
51 changes: 22 additions & 29 deletions src/clipboardbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,6 @@ void ClipboardBrowser::contextMenuEvent(QContextMenuEvent *event)
m_menu->exec( event->globalPos() );
}

void ClipboardBrowser::selectionChanged(const QItemSelection &a,
const QItemSelection &b)
{
QListView::selectionChanged(a, b);
}

void ClipboardBrowser::paintEvent(QPaintEvent *event)
{
d->setDryPaint(true);
Expand Down Expand Up @@ -465,33 +459,30 @@ bool ClipboardBrowser::add(QMimeData *data, bool force)
{
if (!force) {
// don't add if new data is same as first item
if ( m->rowCount() ) {
QStringList formats = data->formats();
QMimeData *olddata = m->mimeData(0);
QStringList oldformats = olddata->formats();
if ( formats.isEmpty() || (oldformats == formats &&
olddata->data(oldformats.at(0)) == data->data(formats.at(0)))
) {
delete data;
return false;
}
if ( *m->at(0) == *data ) {
delete data;
return false;
}

// commands
bool ignore = false;
foreach(const Command &command, commands) {
if (command.automatic || command.ignore) {
foreach(const Command &c, commands) {
if (c.automatic || c.ignore || !c.tab.isEmpty()) {
QString text = data->text();
if (command.re.indexIn(text) != -1) {
if (command.automatic)
emit requestActionDialog(text, &command);
if (command.ignore)
if (c.re.indexIn(text) != -1) {
if (c.automatic)
emit requestActionDialog(text, &c);
if (c.ignore)
ignore = true;
if (!c.tab.isEmpty())
emit addToTab(data, c.tab);
}
}
}
if (ignore)
if (ignore) {
delete data;
return false;
}
}

// create new item
Expand All @@ -513,9 +504,9 @@ bool ClipboardBrowser::add(QMimeData *data, bool force)
return true;
}

bool ClipboardBrowser::add(ClipboardItem *item, bool force)
bool ClipboardBrowser::add(const ClipboardItem &item, bool force)
{
return add( cloneData(*item->data()), force );
return add( cloneData(*item.data()), force );
}

void ClipboardBrowser::loadSettings()
Expand Down Expand Up @@ -543,10 +534,6 @@ void ClipboardBrowser::loadSettings()
void ClipboardBrowser::loadItems()
{
timer_save.stop();

// restore items
m->clear();

ConfigurationManager::instance()->loadItems(*m, m_id);
setCurrentIndex( QModelIndex() );
}
Expand All @@ -562,6 +549,12 @@ void ClipboardBrowser::saveItems(int msec)
ConfigurationManager::instance()->saveItems(*m, m_id);
}

void ClipboardBrowser::purgeItems()
{
ConfigurationManager::instance()->removeItems(m_id);
timer_save.stop();
}

const QString ClipboardBrowser::selectedText() const
{
QString result;
Expand Down
7 changes: 4 additions & 3 deletions src/clipboardbrowser.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ClipboardBrowser : public QListView
*/
bool add(const QString &txt, bool force = false);
bool add(QMimeData *item, bool force = false);
bool add(ClipboardItem *item, bool force = false);
bool add(const ClipboardItem &item, bool force = false);

void remove();

Expand Down Expand Up @@ -101,8 +101,6 @@ class ClipboardBrowser : public QListView
protected:
void keyPressEvent(QKeyEvent *event);
void contextMenuEvent(QContextMenuEvent *);
void selectionChanged ( const QItemSelection & selected,
const QItemSelection & deselected );
void paintEvent(QPaintEvent *event);

signals:
Expand All @@ -115,6 +113,8 @@ class ClipboardBrowser : public QListView
void closeAllEditors();
void changeClipboard(const ClipboardItem *item);

void addToTab(QMimeData *data, const QString &tabName);

private slots:
void realDataChanged(const QModelIndex &a, const QModelIndex &b);
void sizeHintChanged();
Expand All @@ -132,6 +132,7 @@ class ClipboardBrowser : public QListView

void loadItems();
void saveItems(int msec=0);
void purgeItems();

void contextMenuAction(QAction *act);
void updateContextMenu();
Expand Down
27 changes: 22 additions & 5 deletions src/clipboarditem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <QImage>

ClipboardItem::ClipboardItem(const ClipboardModel *parent) :
m_parent(parent), m_filtered(false)
m_parent(parent), m_filtered(false), m_hash(0)
{
m_data = new QMimeData;
}
Expand All @@ -33,17 +33,29 @@ ClipboardItem::~ClipboardItem()
delete m_data;
}

bool ClipboardItem::operator ==(const ClipboardItem &item) const
{
return m_hash == item.m_hash;
}

bool ClipboardItem::operator ==(const QMimeData &data) const
{
return m_hash == hash(data, data.formats());
}

void ClipboardItem::clear()
{
//m_data->clear();
delete m_data;
m_data = new QMimeData;
m_hash = hash(*m_data, m_data->formats());
}

void ClipboardItem::setData(QMimeData *data)
{
delete m_data;
m_data = data;
m_hash = hash(*m_data, m_data->formats());

setPreferredFormat();
}
Expand All @@ -59,11 +71,13 @@ void ClipboardItem::setData(const QVariant &value)
m_data->clear();
m_data->setText( value.toString() );
m_mimeType = QString("text/plain");
m_hash = hash(*m_data, m_data->formats());
}

void ClipboardItem::setData(const QString &mimeType, const QByteArray &data)
{
m_data->setData(mimeType, data);
m_hash = hash(*m_data, m_data->formats());
setPreferredFormat();
}

Expand All @@ -79,7 +93,9 @@ QVariant ClipboardItem::data(int role) const
QPixmap pix;
pixmap(&pix);
return pix;
} else {
} else if ( m_mimeType.endsWith("html") ||
(m_parent && !m_parent->search()->isEmpty()) )
{
return highlightedHtml();
}
} else if (role == Qt::EditRole) {
Expand All @@ -99,13 +115,14 @@ QString ClipboardItem::highlightedHtml() const
re = m_parent ? m_parent->search() : NULL;
if ( !re || re->isEmpty() ) {
// show html if not searching or the text is not too large
if ( m_mimeType.endsWith("html") )
if ( m_mimeType.endsWith("html") ) {
return m_data->html()
.replace("<body>", "<body><span id=\"item\">")
.replace("<!--StartFragment-->",
"<!--StartFragment--><span id=\"item\">");
else
return "<div id=\"item\">" + escape( text() );
} else {
return escape( text() );
}
}

const QString str = text();
Expand Down
6 changes: 6 additions & 0 deletions src/clipboarditem.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class ClipboardItem
ClipboardItem(const ClipboardModel *parent=NULL);
~ClipboardItem();

bool operator ==(const ClipboardItem &item) const;
bool operator ==(const QMimeData &data) const;

QString text() const;

// highlight matched text
Expand All @@ -56,6 +59,8 @@ class ClipboardItem
const QString &format() const { return m_mimeType; }
void setPreferredFormat();

uint dataHash() const { return m_hash; }

bool isFiltered() const { return m_filtered; }
void setFiltered(bool filtered) { m_filtered = filtered; }

Expand All @@ -66,6 +71,7 @@ class ClipboardItem
QMimeData *m_data;
QString m_mimeType;
bool m_filtered;
uint m_hash;
};

QDataStream &operator<<(QDataStream &stream, const ClipboardItem &item);
Expand Down
1 change: 1 addition & 0 deletions src/clipboardmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ QDataStream &operator>>(QDataStream &stream, ClipboardModel &model)
{
int length;
stream >> length;
length = qMin( length, model.maxItems() ) - model.rowCount();

ClipboardItem *item;
for(int i = 0; i < length; ++i) {
Expand Down
4 changes: 1 addition & 3 deletions src/clipboardmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,12 @@ class ClipboardModel : public QAbstractListModel
int role = Qt::EditRole);
bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
void clear() {
m_clipboardList.clear();
}

bool setData(const QModelIndex &index, QMimeData *value);
bool append(ClipboardItem *item);

void setMaxItems(int max);
int maxItems() const { return m_max; }

bool move(int pos, int newpos);
bool moveItems(QModelIndexList list, int key);
Expand Down
23 changes: 8 additions & 15 deletions src/clipboardmonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#endif

ClipboardMonitor::ClipboardMonitor(int &argc, char **argv) :
App(argc, argv), m_newdata(NULL), m_lastHash(0)
App(argc, argv), m_newdata(NULL)
{
m_socket = new QLocalSocket(this);
connect( m_socket, SIGNAL(readyRead()),
Expand All @@ -28,6 +28,11 @@ ClipboardMonitor::ClipboardMonitor(int &argc, char **argv) :
setFormats( cm->value("formats").toString() );
setCheckClipboard( cm->value("check_clipboard").toBool() );

bool ok;
m_lastHash = cm->value("_last_hash").toUInt(&ok);
if (!ok)
m_lastHash = 0;

#ifdef Q_WS_X11
setCopyClipboard( cm->value("copy_clipboard").toBool() );
setCheckSelection( cm->value("check_selection").toBool() );
Expand Down Expand Up @@ -59,18 +64,6 @@ void ClipboardMonitor::setFormats(const QString &list)
m_formats = list.split( QRegExp("[;,\\s]+") );
}

uint ClipboardMonitor::hash(const QMimeData &data)
{
QByteArray bytes;
foreach( QString mime, m_formats ) {
bytes = data.data(mime);
if ( !bytes.isEmpty() )
return qHash(bytes);
}

return 0;
}

#ifdef Q_WS_X11
bool ClipboardMonitor::updateSelection(bool check)
{
Expand Down Expand Up @@ -124,7 +117,7 @@ void ClipboardMonitor::checkClipboard(QClipboard::Mode mode)
}

// same data as last time?
newHash = hash(*data);
newHash = hash(*data, m_formats);
if (m_lastHash == newHash)
return;

Expand Down Expand Up @@ -242,7 +235,7 @@ void ClipboardMonitor::updateClipboard(QMimeData *data, bool force)
if ( !force && m_updatetimer.isActive() )
return;

m_lastHash = hash(*data);
m_lastHash = hash(*data, data->formats());
setClipboardData(data, QClipboard::Clipboard);
#ifdef Q_WS_X11
setClipboardData(cloneData(*data), QClipboard::Selection);
Expand Down
1 change: 0 additions & 1 deletion src/clipboardmonitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class ClipboardMonitor : public App
void setFormats(const QString &list);

void updateClipboard(QMimeData *data, bool force = false);
uint hash(const QMimeData &data);

void setCheckClipboard(bool enable) {m_checkclip = enable;}
void setCheckSelection(bool enable) {m_checksel = enable;}
Expand Down
Loading

0 comments on commit ba2d2d6

Please sign in to comment.