Skip to content
Open
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
35 changes: 27 additions & 8 deletions libdiodon/image-clipboard-item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Diodon
private ClipboardType _clipboard_type;
private string _checksum; // checksum to identify pic content
private Gdk.Pixbuf _pixbuf;
private ByteArray? _raw_payload; // kept for lazy pixbuf loading
private string _label;
private string? _origin;
private DateTime _date_copied;
Expand Down Expand Up @@ -72,10 +73,8 @@ namespace Diodon
_label = label;
_checksum = checksum;

Gdk.PixbufLoader loader = new Gdk.PixbufLoader();
loader.write(payload.data);
loader.close();
_pixbuf = loader.get_pixbuf();
_raw_payload = payload;
_pixbuf = null;
}

/**
Expand Down Expand Up @@ -133,7 +132,7 @@ namespace Diodon
public Icon get_icon()
{
try {
File file = save_tmp_pixbuf(_pixbuf);
File file = save_tmp_pixbuf(ensure_pixbuf());
FileIcon icon = new FileIcon(file);
return icon;
} catch(Error e) {
Expand All @@ -156,7 +155,7 @@ namespace Diodon
*/
public Gtk.Image? get_image()
{
Gdk.Pixbuf pixbuf_preview = create_scaled_pixbuf(_pixbuf);
Gdk.Pixbuf pixbuf_preview = create_scaled_pixbuf(ensure_pixbuf());
return new Gtk.Image.from_pixbuf(pixbuf_preview);
}

Expand All @@ -165,8 +164,11 @@ namespace Diodon
*/
public ByteArray? get_payload() throws GLib.Error
{
if (_raw_payload != null) {
return _raw_payload;
}
uint8[] buffer;
_pixbuf.save_to_buffer(out buffer, "png");
ensure_pixbuf().save_to_buffer(out buffer, "png");
return new ByteArray.take(buffer);
}

Expand All @@ -183,7 +185,7 @@ namespace Diodon
*/
public void to_clipboard(Gtk.Clipboard clipboard)
{
clipboard.set_image(_pixbuf);
clipboard.set_image(ensure_pixbuf());
clipboard.store();
}

Expand Down Expand Up @@ -211,6 +213,23 @@ namespace Diodon
return str_hash(_checksum);
}

private Gdk.Pixbuf ensure_pixbuf()
{
if (_pixbuf != null) {
return _pixbuf;
}
try {
Gdk.PixbufLoader loader = new Gdk.PixbufLoader();
loader.write(_raw_payload.data);
loader.close();
_pixbuf = loader.get_pixbuf();
} catch (Error e) {
warning("Failed to decode image payload: %s", e.message);
_pixbuf = new Gdk.Pixbuf(Gdk.Colorspace.RGB, false, 8, 1, 1);
}
return _pixbuf;
}

/**
* Create a menu icon size scaled pix buf
*
Expand Down
8 changes: 5 additions & 3 deletions libdiodon/text-clipboard-item.vala
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ namespace Diodon
_checksum = Checksum.compute_for_string(ChecksumType.SHA1, _text);
}

// label should not be longer than 50 letters
_label = _text;
string label_text = _text;
if (label_text.length > 200) {
label_text = label_text.substring(0, 200);
}
_label = string.joinv(" ", label_text.split("\n"));
if (_label.char_count() > 50) {
long index_char = _label.index_of_nth_char(50);
_label = _label.substring(0, index_char) + "...";
}
_label = _label.replace("\n", " ");
}

/**
Expand Down