Skip to content
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

.flatpakrepo support #92

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion data/sideload.desktop.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ Type=Application
StartupNotify=true
Categories=PackageManager;
NoDisplay=true
MimeType=application/vnd.flatpak.ref;
MimeType=application/vnd.flatpak.ref;application/vnd.flatpak.repo;
7 changes: 5 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
project(
'io.elementary.sideload',
'io.elementary.sideload',
'vala', 'c',
version: '1.1.0'
)
Expand All @@ -23,13 +23,16 @@ executable(
resources,
'src/Utils/AsyncMutex.vala',
'src/Views/AbstractView.vala',
'src/Views/AddRepoView.vala',
'src/Views/ErrorView.vala',
'src/Views/MainView.vala',
'src/Views/ProgressView.vala',
'src/Views/SuccessView.vala',
'src/AddRepoWindow.vala',
'src/Application.vala',
'src/MainWindow.vala',
'src/FlatpakRefFile.vala',
'src/FlatpakRepoFile.vala',
'src/InstallRefWindow.vala',
dependencies: [
dependency ('flatpak', version: '>=1.1.2'),
dependency ('glib-2.0'),
Expand Down
57 changes: 57 additions & 0 deletions src/AddRepoWindow.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2020 elementary, Inc. (https://elementary.io)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
*/

public class Sideload.AddRepoWindow : Gtk.ApplicationWindow {
public FlatpakRepoFile file { get; construct; }

public AddRepoWindow (Gtk.Application application, FlatpakRepoFile file) {
Object (
application: application,
icon_name: "io.elementary.sideload",
resizable: false,
title: _("Add untrusted software source"),
file: file
);
}

construct {
var titlebar = new Gtk.HeaderBar ();
titlebar.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);
titlebar.set_custom_title (new Gtk.Grid ());

var view = new AddRepoView ();

view.add_requested.connect (() => {
file.add ();
destroy ();
});

add (view);

file.details_ready.connect (() => {
view.display_details (file.get_title ());
});

file.get_details.begin ();

get_style_context ().add_class ("rounded");
set_titlebar (titlebar);
}
}
76 changes: 64 additions & 12 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@
*/

public class Sideload.Application : Gtk.Application {
private MainWindow main_window;
private const string REF_CONTENT_TYPE = "application/vnd.flatpak.ref";
private const string REPO_CONTENT_TYPE = "application/vnd.flatpak.repo";
private const string[] SUPPORTED_CONTENT_TYPES = {
REF_CONTENT_TYPE,
REPO_CONTENT_TYPE
};

public Application () {
Object (
Expand All @@ -39,15 +44,65 @@ public class Sideload.Application : Gtk.Application {
return;
}

var ref_file = new FlatpakRefFile (file);
main_window = new MainWindow (this, ref_file);
main_window.show_all ();
hold ();
open_file.begin (file);
}

var quit_action = new SimpleAction ("quit", null);
var launch_action = new SimpleAction ("launch", null);
private async void open_file (File file) {
GLib.FileInfo? file_info = null;
try {
file_info = yield file.query_info_async (
FileAttribute.STANDARD_CONTENT_TYPE,
FileQueryInfoFlags.NONE
);
} catch (Error e) {
print ("Unable to query content type of provided file\n");
release ();
return;
}

if (file_info == null) {
print ("Unable to query content type of provided file\n");
release ();
return;
}

var content_type = file_info.get_attribute_string (FileAttribute.STANDARD_CONTENT_TYPE);
if (content_type == null) {
print ("Unable to get content type of provided file\n");
release ();
return;
}

if (!(content_type in SUPPORTED_CONTENT_TYPES)) {
print ("This does not appear to be a valid flatpakref/flatpakrepo file\n");
release ();
return;
}

Gtk.ApplicationWindow? main_window = null;

if (content_type == REF_CONTENT_TYPE) {
var ref_file = new FlatpakRefFile (file);
main_window = new InstallRefWindow (this, ref_file);
main_window.show_all ();

var launch_action = new SimpleAction ("launch", null);
add_action (launch_action);

launch_action.activate.connect (() => {
ref_file.launch.begin ();
activate_action ("quit", null);
});
} else if (content_type == REPO_CONTENT_TYPE) {
var repo_file = new FlatpakRepoFile (file);
main_window = new AddRepoWindow (this, repo_file);
main_window.show_all ();
}

var quit_action = new SimpleAction ("quit", null);
add_action (quit_action);
add_action (launch_action);

set_accels_for_action ("app.quit", {"<Control>q"});

quit_action.activate.connect (() => {
Expand All @@ -56,10 +111,7 @@ public class Sideload.Application : Gtk.Application {
}
});

launch_action.activate.connect (() => {
ref_file.launch.begin ();
activate_action ("quit", null);
});
release ();
}

protected override void activate () {
Expand All @@ -77,7 +129,7 @@ public class Sideload.Application : Gtk.Application {

public static int main (string[] args) {
if (args.length < 2) {
print ("Usage: %s /path/to/flatpakref\n", args[0]);
print ("Usage: %s /path/to/flatpakref or /path/to/flatpakrepo\n", args[0]);
return 1;
}

Expand Down
99 changes: 99 additions & 0 deletions src/FlatpakRepoFile.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2020 elementary, Inc. (https://elementary.io)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
*/

public class Sideload.FlatpakRepoFile : Object {
public signal void details_ready ();
public signal void loading_failed ();

public File file { get; construct; }

private static Flatpak.Installation? installation;
private Bytes? bytes = null;

public Flatpak.Remote? remote = null;

static construct {
try {
installation = new Flatpak.Installation.user ();
} catch (Error e) {
warning (e.message);
}
}

public FlatpakRepoFile (File file) {
Object (file: file);
}

public async void get_details () {
var basename = file.get_basename ();

// Build a valid flatpak repo name from the filename
var repo_id = basename.to_ascii ();

// Strip the extension
repo_id = repo_id[0:repo_id.last_index_of (".")];

// Replace any non-alphanumeric characters with underscores
var builder = new StringBuilder ();
for (uint i = 0; repo_id[i] != '\0'; i++) {
if (repo_id[i].isalnum ()) {
builder.append_c (repo_id[i]);
} else {
builder.append_c ('_');
}
}

repo_id = builder.str;

try {
remote = new Flatpak.Remote.from_file (repo_id, yield get_bytes ());
} catch (Error e) {
critical ("Unable to read flatpak repofile, is it valid? Details: %s", e.message);
loading_failed ();
return;
}

details_ready ();
}

public string? get_title () {
return remote.get_title ();
}

public bool add () {
bool success = false;
try {
success = installation.add_remote (remote, true, null);
} catch (Error e) {
warning ("Error adding flatpak remote: %s", e.message);
}

return success;
}

private async Bytes get_bytes () throws Error {
if (bytes != null) {
return bytes;
}

bytes = yield file.load_bytes_async (null, null);
return bytes;
}
}
4 changes: 2 additions & 2 deletions src/MainWindow.vala → src/InstallRefWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
*/

public class Sideload.MainWindow : Gtk.ApplicationWindow {
public class Sideload.InstallRefWindow : Gtk.ApplicationWindow {
public FlatpakRefFile file { get; construct; }
private Cancellable? current_cancellable = null;

Expand All @@ -28,7 +28,7 @@ public class Sideload.MainWindow : Gtk.ApplicationWindow {

private string? app_name = null;

public MainWindow (Gtk.Application application, FlatpakRefFile file) {
public InstallRefWindow (Gtk.Application application, FlatpakRefFile file) {
Object (
application: application,
icon_name: "io.elementary.sideload",
Expand Down
Loading