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

Add button to clear queue #163

Merged
merged 15 commits into from
Jun 28, 2022
39 changes: 39 additions & 0 deletions src/Dialogs/ClearQueueDialog.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*-
* Copyright (c) 2022 elementary LLC. (https://elementary.io)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/

public class Printers.ClearQueueDialog : Granite.MessageDialog {
public Printer printer { get; construct; }

public ClearQueueDialog (Printer printer) {
Object (
buttons: Gtk.ButtonsType.CANCEL,
image_icon: new ThemedIcon ("edit-clear"),
badge_icon: new ThemedIcon ("dialog-question"),
modal: true,
printer: printer,
primary_text: _("Clear all pending and completed jobs from “%s”?").printf (printer.info),
secondary_text: _("All unfinished jobs will be canceled and all print history will be erased.")
);
}

construct {
var button = add_button (_("Clear Queue"), Gtk.ResponseType.OK);
button.get_style_context ().add_class (Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION);
}
}
15 changes: 15 additions & 0 deletions src/Objects/Job.vala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ public class Printers.Job : GLib.Object {
public DateTime creation_time { get; construct; }
public DateTime? completed_time { get; set; default = null; }

public bool canceled_or_aborted {
get {
return (state in CUPS.IPP.JobState.CANCELED) ||
(state in CUPS.IPP.JobState.ABORTED);
}
}

public Job (CUPS.Job cjob, Printer printer) {
Object (
creation_time: cjob.creation_time > 0 ? new DateTime.from_unix_local (cjob.creation_time) : new DateTime.now (),
Expand Down Expand Up @@ -85,6 +92,14 @@ public class Printers.Job : GLib.Object {
}
}

public void purge () {
try {
Cups.get_pk_helper ().job_cancel_purge (uid, true);
} catch (Error e) {
critical (e.message);
}
}

public void resume () {
try {
Cups.get_pk_helper ().job_set_hold_until (uid, "no-hold");
Expand Down
12 changes: 11 additions & 1 deletion src/PrinterPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public class Printers.PrinterPage : Granite.SimpleSettingsPage {

construct {
var stack = new Gtk.Stack ();
stack.add_titled (new JobsView (printer), "general", _("Print Queue"));
var jobs_view = new JobsView (printer);
stack.add_titled (jobs_view, "general", _("Print Queue"));
stack.add_titled (new OptionsPage (printer), "options", _("Page Setup"));
stack.add_titled (new SuppliesView (printer), "supplies", _("Settings & Supplies"));

Expand All @@ -51,6 +52,15 @@ public class Printers.PrinterPage : Granite.SimpleSettingsPage {
var print_test = new Gtk.Button.with_label (_("Print Test Page"));
print_test.clicked.connect (() => print_test_page ());

var clear_queue_button = new Gtk.Button.with_label (_("Clear Queue"));
clear_queue_button.get_style_context ().add_class (Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION);
clear_queue_button.sensitive = jobs_view.n_jobs > 0;
jobs_view.notify["n-jobs"].connect (() => {
clear_queue_button.sensitive = jobs_view.n_jobs > 0;
});
clear_queue_button.clicked.connect (() => jobs_view.clear_queue ());

action_area.add (clear_queue_button);
action_area.add (print_test);

printer.bind_property ("info", this, "title");
Expand Down
46 changes: 40 additions & 6 deletions src/Views/JobsView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
public class Printers.JobsView : Gtk.Frame {
private Printer printer;
private Gtk.ListBox list_box;
public uint n_jobs { get; private set; }

public JobsView (Printer printer) {
this.printer = printer;
Expand All @@ -40,14 +41,10 @@ public class Printers.JobsView : Gtk.Frame {
scrolled.expand = true;
scrolled.add (list_box);
scrolled.show_all ();

var jobs = printer.get_jobs (true, CUPS.WhichJobs.ALL);
foreach (var job in jobs) {
list_box.add (new JobRow (printer, job));
}

add (scrolled);

refresh_job_list ();

unowned Cups.Notifier notifier = Cups.Notifier.get_default ();
notifier.job_created.connect ((text, printer_uri, name, state, state_reasons, is_accepting_jobs, job_id, job_state, job_state_reason, job_name, job_impressions_completed) => {
if (printer.dest.name != name) {
Expand All @@ -61,6 +58,8 @@ public class Printers.JobsView : Gtk.Frame {
break;
}
}

n_jobs = list_box.get_children ().length ();
});
}

Expand Down Expand Up @@ -99,4 +98,39 @@ public class Printers.JobsView : Gtk.Frame {
row1.set_header (null);
}
}

public void clear_queue () {
var dialog = new ClearQueueDialog (printer) {
transient_for = (Gtk.Window) get_toplevel ()
};
dialog.response.connect ((response_id) => {
dialog.destroy ();

if (response_id == Gtk.ResponseType.OK) {
list_box.@foreach ((row) => {
var job = ((JobRow)row).job;
job.purge ();
});

refresh_job_list ();
}
});

dialog.present ();
}

private void refresh_job_list () {
list_box.@foreach ((row) => {
list_box.remove (row);
});

var jobs = printer.get_jobs (true, CUPS.WhichJobs.ALL);
foreach (var job in jobs) {
if (!job.canceled_or_aborted) {
danirabbit marked this conversation as resolved.
Show resolved Hide resolved
list_box.add (new JobRow (printer, job));
}
}

n_jobs = list_box.get_children ().length ();
}
}
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plug_files = files(
'CUPSPkHelper.vala',
'CUPSNotifier.vala',
'Dialogs/AddDialog.vala',
'Dialogs/ClearQueueDialog.vala',
'Dialogs/RemoveDialog.vala',
'Objects/DeviceDriver.vala',
'Objects/Printer.vala',
Expand Down