Skip to content

Instantly share code, notes, and snippets.

@maurges
Created November 16, 2019 11:22
Show Gist options
  • Select an option

  • Save maurges/dbda9569e9f1dfcd673b27984eb5f3cb to your computer and use it in GitHub Desktop.

Select an option

Save maurges/dbda9569e9f1dfcd673b27984eb5f3cb to your computer and use it in GitHub Desktop.
Clipboard Explorer: see what files hide in clipboard
#include "mainwindow.h"
#include <QApplication>
// Description: simple application with intuitive interface.
// It dumps content of clipboard into files. Useful when you have many different content
// types in your clipboard, and want to see them all.
// All output goes into console window, so run from console.
// Requires Qt5Core and Qt5Widgets. Can be built with qmake or cmake (I build with qmake).
int main(int argc, char *argv[])
{
QApplication a (argc, argv);
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QGuiApplication>
#include <QClipboard>
#include <QMimeData>
#include <QPixmap>
#include <QPushButton>
#include <QBoxLayout>
#include <QFile>
#include <QDebug>
void infoClicked()
{
auto* data = QGuiApplication::clipboard()->mimeData();
qDebug() << "Available formats:";
auto formats = data->formats();
for (const auto& format : formats)
{
qDebug() << format;
}
}
void dumpClicked()
{
auto* data = QGuiApplication::clipboard()->mimeData();
auto formats = data->formats();
if (data->hasImage())
{
auto&& file = QFile("image_data");
if (not file.open(QIODevice::WriteOnly))
{
qDebug() << "could not write image";
}
else
{
file.write(data->imageData().toByteArray());
qDebug() << "written image_data";
}
}
for (const auto& format : formats)
{
if (format == "TARGETS" or format == "MULTIPLE" or format == "TIMESTAMP" or format == "SAVE_TARGETS") continue;
auto&& content = data->data(format);
if (format.contains("image/"))
{
// as we've written image above and there are a lot of them
continue;
}
if (format == "text/plain" or format == "text/html" or format == "text/uri-list")
{
qDebug(content);
}
else
{
if (content.isEmpty())
{
qDebug() << "data for" << format << "is empty";
continue;
}
auto&& name = QString(format);
name.replace(QChar('/'), QChar('%'));
auto&& file = QFile(name);
if (not file.open(QIODevice::ReadWrite))
{
qDebug() << "error writing to file" << name;
continue;
}
auto size = file.write(content);
file.flush(); file.close();
qDebug() << "written" << size << "bytes to" << name;
}
}
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setMinimumSize(400, 400);
auto* central = new QWidget(this);
auto* layout = new QBoxLayout(QBoxLayout::TopToBottom, central);
auto* infoButton = new QPushButton("List mime types", this);
connect(infoButton, &QPushButton::clicked, &infoClicked);
layout->addWidget(infoButton);
auto* dumpButton = new QPushButton("Dump data", this);
connect(dumpButton, &QPushButton::clicked, &dumpClicked);
layout->addWidget(dumpButton);
central->setLayout(layout);
this->setCentralWidget(central);
}
#pragma once
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow() = default;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment