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

Use Gtk.DropDown #75

Draft
wants to merge 4 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 .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Install Dependencies
run: |
apt update
apt install -y libgranite-7-dev libgtk-4-dev libpolkit-agent-1-dev libpolkit-gobject-1-dev meson valac
apt install -y libaccountsservice-dev libadwaita-1-dev libgranite-7-dev libgtk-4-dev libpolkit-agent-1-dev libpolkit-gobject-1-dev meson valac
- name: Build
env:
DESTDIR: out
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
## Building, Testing, and Installation

You'll need the following dependencies:
* accountsservice
* libgranite-7-dev (>= 7.0.0)
* libgtk-4-dev
* libadwaita-1-dev
* libpolkit-gobject-1-dev
* libpolkit-agent-1-dev
* meson
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ executable(
'src/PolkitDialog.vala',
'src/Interfaces.vala',
dependencies: [
dependency('accountsservice'),
dependency('glib-2.0'),
dependency('gobject-2.0'),
dependency('granite-7', version: '>=7.0.0'),
dependency('gtk4'),
dependency('libadwaita-1'),
dependency('polkit-agent-1'),
dependency('polkit-gobject-1'),
meson.get_compiler('vala').find_library('posix')
Expand Down
147 changes: 75 additions & 72 deletions src/PolkitDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ public class Ag.PolkitDialog : Granite.MessageDialog {
private Gtk.Label password_label;
private Gtk.Label password_feedback;
private Gtk.Entry password_entry;
private Gtk.ComboBox idents_combo;
private Gtk.DropDown dropdown;

private static Act.UserManager usermanager;

static construct {
usermanager = Act.UserManager.get_default ();
}

public PolkitDialog (string message, string icon_name, string _cookie,
List<Polkit.Identity?>? _idents, GLib.Cancellable _cancellable) {
Expand Down Expand Up @@ -80,23 +86,19 @@ public class Ag.PolkitDialog : Granite.MessageDialog {
child = password_feedback
};

idents_combo = new Gtk.ComboBox () {
hexpand = true
};
idents_combo.changed.connect (on_ident_changed);
var list_factory = new Gtk.SignalListItemFactory ();
list_factory.setup.connect (setup_factory);
list_factory.bind.connect (bind_factory);

Gtk.CellRenderer renderer = new Gtk.CellRendererPixbuf ();
idents_combo.pack_start (renderer, false);
idents_combo.add_attribute (renderer, "icon-name", 0);
var model = new ListStore (typeof (Polkit.Identity));

renderer = new Gtk.CellRendererText ();
renderer.xpad = 6;
idents_combo.pack_start (renderer, true);
idents_combo.add_attribute (renderer, "text", 1);
idents_combo.set_id_column (1);
dropdown = new Gtk.DropDown (model, null) {
factory = list_factory,
hexpand = true
};

var credentials_box = new Gtk.Box (VERTICAL, 6);
credentials_box.append (idents_combo);
credentials_box.append (dropdown);
credentials_box.append (password_entry);
credentials_box.append (feedback_revealer);

Expand All @@ -121,61 +123,79 @@ public class Ag.PolkitDialog : Granite.MessageDialog {

close.connect (cancel);

update_idents ();
dropdown.notify["selected"].connect (() => {
pk_identity = (Polkit.Identity) dropdown.get_selected_item ();
select_session ();
});

foreach (unowned Polkit.Identity? identity in idents) {
if (identity != null) {
model.append (identity);
}
}

if (model.get_n_items () < 2) {
// if (target_user == Environment.get_user_name ()) {
// dropdown.visible = false;
// } else {
// dropdown.sensitive = false;
// }
}

select_session ();
}

private void update_idents () {
var model = new Gtk.ListStore (3, typeof (string), typeof (string), typeof (Polkit.Identity));
Gtk.TreeIter iter;
private void setup_factory (Object object) {
var avatar = new Adw.Avatar (24, null, true);

int length = 0;
int active = 0;
var user_name = new Gtk.Label ("");

string? target_user = null;
var box = new Gtk.Box (HORIZONTAL, 6);
box.append (avatar);
box.append (user_name);

foreach (unowned Polkit.Identity? ident in idents) {
if (ident == null) {
continue;
}
var list_item = (Gtk.ListItem) object;
list_item.set_data ("avatar", avatar);
list_item.set_data ("username", user_name);
list_item.set_child (box);
}

string name = ident.to_string ();
private void bind_factory (Object object) {
var list_item = (Gtk.ListItem) object;

if (ident is Polkit.UnixUser) {
unowned Posix.Passwd? pwd = Posix.getpwuid (((Polkit.UnixUser)ident).get_uid ());
if (pwd != null) {
string pw_name = pwd.pw_name;
if (target_user == null && length < 2) {
target_user = pw_name;
}
var identity = (Polkit.Identity) list_item.get_item ();

name = pw_name;
}
} else if (ident is Polkit.UnixGroup) {
unowned Posix.Group? gwd = Posix.getgrgid (((Polkit.UnixGroup)ident).get_gid ());
if (gwd != null) {
name = _("Group: %s").printf (gwd.gr_name);
}
}
var user_name = list_item.get_data<Gtk.Label>("username");
user_name.label = identity.to_string ();

model.append (out iter);
model.set (iter, 0, "avatar-default-symbolic", 1, name, 2, ident);
var avatar = list_item.get_data<Adw.Avatar>("avatar");

if (name == Environment.get_user_name ()) {
active = length;
}
if (identity is Polkit.UnixUser) {
unowned Posix.Passwd? pwd = Posix.getpwuid (((Polkit.UnixUser)identity).get_uid ());
if (pwd != null) {
string pw_name = pwd.pw_name;

length++;
}
if (usermanager.is_loaded) {
var user = usermanager.get_user (pw_name);

idents_combo.set_model (model);
idents_combo.active = active;
user_name.label = user.get_real_name ();

if (length < 2) {
if (target_user == Environment.get_user_name ()) {
idents_combo.visible = false;
} else {
idents_combo.sensitive = false;
var user_icon_file = File.new_for_path (user.get_icon_file ());
if (user_icon_file.query_exists ()) {
avatar.set_custom_image (new Gtk.IconPaintable.for_file (user_icon_file, avatar.size, 1));
} else {
avatar.text = user.get_real_name ();
}
} else {
user_name.label = pw_name;
avatar.text = pw_name;
}
}
} else if (identity is Polkit.UnixGroup) {
unowned Posix.Group? gwd = Posix.getgrgid (((Polkit.UnixGroup)identity).get_gid ());
if (gwd != null) {
user_name.label = _("Group: %s").printf (gwd.gr_name);
avatar.text = gwd.gr_name;
}
}
}
Expand Down Expand Up @@ -228,23 +248,6 @@ public class Ag.PolkitDialog : Granite.MessageDialog {
done ();
}

private void on_ident_changed () {
Gtk.TreeIter iter;

if (!idents_combo.get_active_iter (out iter)) {
deselect_session ();
return;
}

var model = idents_combo.get_model ();
if (model == null) {
return;
}

model.get (iter, 2, out pk_identity, -1);
select_session ();
}

private void on_pk_session_completed (bool authorized) {
sensitive = true;
if (!authorized || cancellable.is_cancelled ()) {
Expand Down
Loading