Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions libdiodon/file-clipboard-item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,16 @@ namespace Diodon
* @param data paths separated with \n
* @param origin origin of clipboard item as application path
*/
public FileClipboardItem(ClipboardType clipboard_type, string data, string? origin, DateTime date_copied) throws FileError
public FileClipboardItem(ClipboardType clipboard_type, string data, string? origin, DateTime date_copied, string? checksum = null) throws FileError
{
_clipboard_type = clipboard_type;
_paths = data;
_origin = origin;
_date_copied = date_copied;
_checksum = Checksum.compute_for_string(ChecksumType.SHA1, _paths);
_checksum = checksum;
if(_checksum == null) {
_checksum = Checksum.compute_for_string(ChecksumType.SHA1, _paths);
}

// check if all paths are available
string[] paths = convert_to_paths(_paths);
Expand Down
36 changes: 14 additions & 22 deletions libdiodon/image-clipboard-item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,37 @@ namespace Diodon
_clipboard_type = clipboard_type;
_origin = origin;
_date_copied = date_copied;
extract_pixbuf_info(pixbuf);

// create checksum of picture
Checksum checksum = new Checksum(ChecksumType.SHA1);
checksum.update(pixbuf.get_pixels(), pixbuf.height * pixbuf.rowstride);
_checksum = checksum.get_string().dup();

// label in format [{width}x{height}]
_label ="[%dx%d]".printf(pixbuf.width, pixbuf.height);
_pixbuf = pixbuf;
}

/**
* Create image clipboard item by given payload.
*
* @param clipboard_type clipboard type item is coming from
* @param clipboard_type label how it will be shown to user
* @param pixbuf image from clipboard
* @param origin origin of clipboard item as application path
*/
public ImageClipboardItem.with_payload(ClipboardType clipboard_type, ByteArray payload, string? origin, DateTime date_copied) throws GLib.Error
public ImageClipboardItem.with_payload(ClipboardType clipboard_type, string label, ByteArray payload, string? origin, DateTime date_copied, string checksum) throws GLib.Error
{
_clipboard_type = clipboard_type;
_origin = origin;
_date_copied = date_copied;
_label = label;
_checksum = checksum;

Gdk.PixbufLoader loader = new Gdk.PixbufLoader();
loader.write(payload.data);
loader.close();
Gdk.Pixbuf pixbuf = loader.get_pixbuf();
extract_pixbuf_info(pixbuf);
_pixbuf = loader.get_pixbuf();
}

/**
Expand Down Expand Up @@ -201,24 +211,6 @@ namespace Diodon
return str_hash(_checksum);
}

/**
* Extracts all pixbuf information which are needed to show image
* in the view without having the pixbuf in the memory.
*
* @param pixbuf pixbuf to extract info from
*/
private void extract_pixbuf_info(Gdk.Pixbuf pixbuf)
{
// create checksum of picture
Checksum checksum = new Checksum(ChecksumType.SHA1);
checksum.update(pixbuf.get_pixels(), pixbuf.height * pixbuf.rowstride);
_checksum = checksum.get_string().dup();

// label in format [{width}x{height}]
_label ="[%dx%d]".printf(pixbuf.width, pixbuf.height);
_pixbuf = pixbuf;
}

/**
* Create a menu icon size scaled pix buf
*
Expand Down
7 changes: 5 additions & 2 deletions libdiodon/text-clipboard-item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,16 @@ namespace Diodon
* @param data simple text
* @param origin origin of clipboard item as application path
*/
public TextClipboardItem(ClipboardType clipboard_type, string data, string? origin, DateTime date_copied)
public TextClipboardItem(ClipboardType clipboard_type, string data, string? origin, DateTime date_copied, string? checksum = null)
{
_clipboard_type = clipboard_type;
_text = data;
_origin = origin;
_date_copied = date_copied;
_checksum = Checksum.compute_for_string(ChecksumType.SHA1, _text);
_checksum = checksum;
if(_checksum == null) {
_checksum = Checksum.compute_for_string(ChecksumType.SHA1, _text);
}

// label should not be longer than 50 letters
_label = _text;
Expand Down
7 changes: 4 additions & 3 deletions libdiodon/zeitgeist-clipboard-storage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -532,21 +532,22 @@ namespace Diodon
string interpretation = subject.interpretation;
IClipboardItem item = null;
string text = subject.text;
string checksum = subject.uri.split(":")[1];
string? origin = subject.origin;
unowned ByteArray payload = event.payload;
DateTime date_copied = new DateTime.from_unix_utc(event.timestamp);

try {
if(strcmp(NFO.PLAIN_TEXT_DOCUMENT, interpretation) == 0) {
item = new TextClipboardItem(ClipboardType.NONE, text, origin, date_copied);
item = new TextClipboardItem(ClipboardType.NONE, text, origin, date_copied, checksum);
}

else if(strcmp(NFO.FILE_DATA_OBJECT, interpretation) == 0) {
item = new FileClipboardItem(ClipboardType.NONE, text, origin, date_copied);
item = new FileClipboardItem(ClipboardType.NONE, text, origin, date_copied, checksum);
}

else if(strcmp(NFO.IMAGE, interpretation) == 0) {
item = new ImageClipboardItem.with_payload(ClipboardType.NONE, payload, origin, date_copied);
item = new ImageClipboardItem.with_payload(ClipboardType.NONE, text, payload, origin, date_copied, checksum);
}

else {
Expand Down
5 changes: 4 additions & 1 deletion tests/test-image-clipboard-item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ namespace Diodon
ImageClipboardItem item1 = new ImageClipboardItem.with_image(ClipboardType.CLIPBOARD, pixbuf, null, new DateTime.now_utc());
string checksum1 = item1.get_checksum();

ImageClipboardItem item2 = new ImageClipboardItem.with_payload(ClipboardType.CLIPBOARD, item1.get_payload(), null, new DateTime.now_utc());
ImageClipboardItem item2 = new ImageClipboardItem.with_payload(
ClipboardType.CLIPBOARD, "[64x64]", item1.get_payload(), null, new DateTime.now_utc(), item1.get_checksum()
);
string checksum2 = item2.get_checksum();

FsoFramework.Test.Assert.are_equal_string(checksum1, checksum2, "Images are not the same");
FsoFramework.Test.Assert.are_equal_string(item1.get_label(), item2.get_label(), "Labels are not the same");
}
}
}
Expand Down