-
-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Imgur upload functionality #311
base: main
Are you sure you want to change the base?
Changes from 23 commits
0c4eeb4
d962cf3
c5ba2fb
ea6c9a9
1328aa4
83fd79b
1deb39c
9465011
ea3ee1c
87128ee
62d5030
6e0bba7
59625f4
d5b1bf7
c6768e5
853d91d
9495360
e9576c6
5e96ae5
686d640
465e5ce
dd0fee9
97c3253
d07328f
f790dfd
0eaa85c
37bbc01
c738a54
f888bca
81ce285
74fd725
c1d169b
3cbbd4e
625701b
e3db2c5
5a63323
f9bf8d2
2da5d2f
bedf997
979fb47
d650a43
4aa0242
0e0fd6b
a5ecf1d
f2771e3
abfc727
4265d33
da7f7f5
fb49937
e5c43b7
aacdc10
22908f9
ef0480e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
|
||
using Gtk; | ||
using Soup; | ||
|
||
namespace Peek.Ui { | ||
|
||
[GtkTemplate (ui = "/com/uploadedlobster/peek/share-dialog.ui")] | ||
class ShareDialog : Window { | ||
private static Gtk.Window? instance; | ||
|
||
public static Gtk.Window present_single_instance (Gtk.Window main_window) { | ||
if (instance == null) { | ||
instance = new ShareDialog (); | ||
instance.delete_event.connect ((event) => { | ||
instance = null; | ||
main_window.set_keep_above (true); | ||
return false; | ||
}); | ||
} | ||
|
||
instance.transient_for = main_window; | ||
main_window.set_keep_above (false); | ||
instance.present (); | ||
return instance; | ||
} | ||
|
||
public static string file { get; set; } | ||
public static string file_type { get; set; } | ||
|
||
public static void filename (string out_file) { | ||
file=out_file; | ||
} | ||
|
||
public static void get_file_ext (string file_ext) { | ||
file_type=file_ext; | ||
} | ||
|
||
[GtkChild] | ||
private Gtk.Image check_1; | ||
|
||
[GtkChild] | ||
private Gtk.Image check_2; | ||
|
||
[GtkChild] | ||
private Gtk.ListBox options_list; | ||
|
||
[GtkChild] | ||
private Gtk.ListBoxRow row_1; | ||
|
||
[GtkChild] | ||
private Gtk.ListBoxRow row_2; | ||
|
||
[GtkCallback] | ||
private void on_options_list_row_selected () { | ||
var selection = options_list.get_selected_row (); | ||
if(file_type == "webm" || file_type == "mp4"){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it is better to completely hide the row if it's not available for other formats. It was not completely clear to me why the Imgur option was not selectable. Also it might be better to whitelist formats, since this avoids breaking things if new formats are added to Peek. |
||
row_1.set_selectable(false); | ||
check_2.show(); | ||
check_1.hide(); | ||
options_list.select_row(row_2); | ||
debug("WebM and Mp4 files are not supported with in imgur upload api"); | ||
}else { | ||
row_1.set_selectable(true); | ||
check_1.show(); | ||
if (selection == row_1) { | ||
check_2.hide(); | ||
}else if (selection == row_2 ){ | ||
check_2.show(); | ||
check_1.hide(); | ||
} | ||
} | ||
|
||
|
||
} | ||
[GtkCallback] | ||
private void on_confirm_option_clicked() { | ||
if (options_list.get_selected_row () == row_1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to give the rows descriptive names, e.g. "imgur_row". Both here in the code and in the .ui file. Also I would put the entire Imgur specific code into a separate function like |
||
string image_uri = file; | ||
string image=image_uri.replace("%20"," "); | ||
debug(image); | ||
uint8[] data; | ||
|
||
try { | ||
GLib.FileUtils.get_data(image.split("://")[1], out data); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you use |
||
} catch (GLib.FileError e) { | ||
warning(e.message); | ||
} | ||
|
||
string image_test =GLib.Base64.encode(data); | ||
var mpart = new Multipart(FORM_MIME_TYPE_MULTIPART); | ||
var session = new Soup.Session (); | ||
mpart.append_form_string("image", image_test); | ||
var message = Soup.Form.request_new_from_multipart("https://api.imgur.com/3/image",mpart); | ||
message.request_headers.append("Authorization", "Client-ID bfb3ac58837a0d0"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would put the client ID into a constant |
||
session.send_message(message); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using Soup.Session.queue_message for asynchroneously sending the message and not block here. You will need to put the rest of this function into the callback. |
||
var response = (string) message.response_body.data; | ||
debug(response); | ||
try { | ||
var parser = new Json.Parser (); | ||
parser.load_from_data (response, -1); | ||
var root_object = parser.get_root ().get_object (); | ||
string link = root_object.get_object_member ("data") | ||
.get_string_member ("link"); | ||
debug(link); | ||
#if HAS_GTK_SHOW_URI_ON_WINDOW | ||
Gtk.show_uri_on_window(instance, link, Gdk.CURRENT_TIME); | ||
#else | ||
Gtk.show_uri(null, link, Gdk.CURRENT_TIME); | ||
#endif | ||
|
||
|
||
this.close(); | ||
}catch(Error e) { | ||
error("%s", e.message); | ||
} | ||
} | ||
} | ||
[GtkCallback] | ||
private void on_sharing_cancel_clicked() { | ||
this.close (); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- Generated with glade 3.22.1 --> | ||
<interface> | ||
<requires lib="gtk+" version="3.14"/> | ||
<template class="PeekUiShareDialog" parent="GtkWindow"> | ||
<property name="can_focus">False</property> | ||
<property name="border_width">0</property> | ||
<property name="modal">True</property> | ||
<property name="window_position">center-on-parent</property> | ||
<property name="gravity">center</property> | ||
<child type="titlebar"> | ||
<object class="GtkHeaderBar" id="headerbar1"> | ||
<property name="visible">True</property> | ||
<property name="can_focus">False</property> | ||
<property name="hexpand">True</property> | ||
<property name="spacing">4</property> | ||
<child type="title"> | ||
<object class="GtkLabel"> | ||
<property name="visible">True</property> | ||
<property name="can_focus">False</property> | ||
<property name="label" translatable="yes">Peek Sharing</property> | ||
</object> | ||
</child> | ||
<child> | ||
<object class="GtkButton" id="confirm_option"> | ||
<property name="label" translatable="yes">OK</property> | ||
<property name="visible">True</property> | ||
<property name="can_focus">True</property> | ||
<property name="receives_default">True</property> | ||
<property name="hexpand">False</property> | ||
<signal name="clicked" handler="on_confirm_option_clicked" swapped="no"/> | ||
</object> | ||
<packing> | ||
<property name="pack_type">end</property> | ||
</packing> | ||
</child> | ||
<child> | ||
<object class="GtkButton" id="sharing_cancel"> | ||
<property name="label">gtk-cancel</property> | ||
<property name="visible">True</property> | ||
<property name="can_focus">True</property> | ||
<property name="receives_default">True</property> | ||
<property name="use_stock">True</property> | ||
<signal name="clicked" handler="on_sharing_cancel_clicked" swapped="no"/> | ||
</object> | ||
<packing> | ||
<property name="pack_type">end</property> | ||
<property name="position">1</property> | ||
</packing> | ||
</child> | ||
</object> | ||
</child> | ||
<child> | ||
<object class="GtkListBox" id="options_list"> | ||
<property name="width_request">500</property> | ||
<property name="height_request">100</property> | ||
<property name="visible">True</property> | ||
<property name="can_focus">False</property> | ||
<property name="margin_left">6</property> | ||
<property name="margin_right">6</property> | ||
<property name="margin_top">6</property> | ||
<property name="margin_bottom">6</property> | ||
<signal name="row-selected" handler="on_options_list_row_selected" swapped="no"/> | ||
<child> | ||
<object class="GtkListBoxRow" id="row_1"> | ||
<property name="width_request">100</property> | ||
<property name="height_request">80</property> | ||
<property name="visible">True</property> | ||
<property name="can_focus">True</property> | ||
<property name="activatable">False</property> | ||
<child> | ||
<object class="GtkBox"> | ||
<property name="visible">True</property> | ||
<property name="can_focus">False</property> | ||
<property name="border_width">5</property> | ||
<child> | ||
<object class="GtkLabel"> | ||
<property name="visible">True</property> | ||
<property name="can_focus">False</property> | ||
<property name="label" translatable="yes"><b>Upload to Imgur</b> | ||
<span size='small'>Upload to imgur for easy sharing</span></property> | ||
<property name="use_markup">True</property> | ||
</object> | ||
<packing> | ||
<property name="expand">False</property> | ||
<property name="fill">True</property> | ||
<property name="position">0</property> | ||
</packing> | ||
</child> | ||
<child> | ||
<object class="GtkImage" id="check_1"> | ||
<property name="can_focus">False</property> | ||
<property name="halign">end</property> | ||
<property name="hexpand">True</property> | ||
<property name="icon_name">object-select-symbolic</property> | ||
</object> | ||
<packing> | ||
<property name="expand">False</property> | ||
<property name="fill">True</property> | ||
<property name="position">1</property> | ||
</packing> | ||
</child> | ||
</object> | ||
</child> | ||
</object> | ||
</child> | ||
<child> | ||
<object class="GtkListBoxRow" id="row_2"> | ||
<property name="width_request">100</property> | ||
<property name="height_request">80</property> | ||
<property name="visible">True</property> | ||
<property name="can_focus">True</property> | ||
<property name="activatable">False</property> | ||
<child> | ||
<object class="GtkBox"> | ||
<property name="visible">True</property> | ||
<property name="can_focus">False</property> | ||
<property name="border_width">5</property> | ||
<child> | ||
<object class="GtkLabel"> | ||
<property name="visible">True</property> | ||
<property name="can_focus">False</property> | ||
<property name="label" translatable="yes"><b>Open in File Manger</b> | ||
<span size='small'>Open the saved file in your file manager.</span></property> | ||
<property name="use_markup">True</property> | ||
</object> | ||
<packing> | ||
<property name="expand">False</property> | ||
<property name="fill">True</property> | ||
<property name="position">0</property> | ||
</packing> | ||
</child> | ||
<child> | ||
<object class="GtkImage" id="check_2"> | ||
<property name="can_focus">False</property> | ||
<property name="halign">end</property> | ||
<property name="hexpand">True</property> | ||
<property name="icon_name">object-select-symbolic</property> | ||
</object> | ||
<packing> | ||
<property name="expand">False</property> | ||
<property name="fill">True</property> | ||
<property name="position">1</property> | ||
</packing> | ||
</child> | ||
</object> | ||
</child> | ||
</object> | ||
</child> | ||
</object> | ||
</child> | ||
</template> | ||
</interface> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably be easier and more flexible to pass the
File
object to theShareDialog
instead of just the URI. Also useFile.get_path()
instead offile.get_uri()
, see my comment below.