| 1 | /* Procman - dialogs |
| 2 | * Copyright (C) 2001 Kevin Vandersloot |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version 2 |
| 7 | * of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public |
| 15 | * License along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <config.h> |
| 20 | |
| 21 | #include <glib/gi18n.h> |
| 22 | |
| 23 | #include <signal.h> |
| 24 | #include <string.h> |
| 25 | #include <stdio.h> |
| 26 | |
| 27 | #include "procdialogs.h" |
| 28 | #include "application.h" |
| 29 | #include "proctable.h" |
| 30 | #include "prettytable.h" |
| 31 | #include "procactions.h" |
| 32 | #include "procinfo.h" |
| 33 | #include "util.h" |
| 34 | #include "gsm-cgroups.h" |
| 35 | |
| 36 | static AdwAlertDialog *renice_dialog = NULL; |
| 37 | static gint new_nice_value = 0; |
| 38 | |
| 39 | |
| 40 | struct ProcActionArgs { |
| 41 | GsmApplication *app; |
| 42 | int arg_value; |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | static GtkWindow * |
| 47 | get_active_window () |
| 48 | { |
| 49 | GListModel *toplevels = gtk_window_get_toplevels (); |
| 50 | |
| 51 | guint n_toplevels = g_list_model_get_n_items (toplevels); |
| 52 | for (guint i = 0; i < n_toplevels; i++) |
| 53 | { |
| 54 | GtkWindow *window = GTK_WINDOW (g_list_model_get_item (toplevels, i)); |
| 55 | |
| 56 | if (gtk_window_is_active (window)) |
| 57 | return window; |
| 58 | } |
| 59 | |
| 60 | g_object_unref (toplevels); |
| 61 | |
| 62 | return NULL; |
| 63 | } |
| 64 | |
| 65 | static void |
| 66 | kill_dialog_choose (GObject *dialog, |
| 67 | GAsyncResult *res, |
| 68 | gpointer data) |
| 69 | { |
| 70 | struct ProcActionArgs *kargs = static_cast<ProcActionArgs*>(data); |
| 71 | |
| 72 | const gchar *res_id = adw_alert_dialog_choose_finish (ADW_ALERT_DIALOG (dialog), res); |
| 73 | |
| 74 | if (g_str_equal (res_id, adw_alert_dialog_get_close_response (ADW_ALERT_DIALOG (dialog)))) |
| 75 | return; |
| 76 | |
| 77 | kill_process (kargs->app, kargs->arg_value, -1); |
| 78 | |
| 79 | proctable_thaw (kargs->app); |
| 80 | proctable_update (kargs->app); |
| 81 | g_free (kargs); |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | procdialog_create_kill_dialog (GsmApplication *app, |
| 86 | int signal, |
| 87 | gint32 proc) |
| 88 | { |
| 89 | AdwAlertDialog *kill_alert_dialog; |
| 90 | struct ProcActionArgs *kargs; |
| 91 | gchar *primary, *secondary, *button_text; |
| 92 | GCancellable *cancellable; |
| 93 | |
| 94 | proctable_freeze (app); |
| 95 | kargs = g_new (ProcActionArgs, 1); |
| 96 | kargs->app = app; |
| 97 | kargs->arg_value = signal; |
| 98 | gint selected_count = gtk_tree_selection_count_selected_rows (app->selection); |
| 99 | |
| 100 | if (selected_count == 1 || proc != -1) |
| 101 | { |
| 102 | ProcInfo *selected_process = NULL; |
| 103 | |
| 104 | if (proc == -1) |
| 105 | { |
| 106 | // get the last selected row |
| 107 | gtk_tree_selection_selected_foreach (app->selection, get_last_selected, |
| 108 | &selected_process); |
| 109 | } |
| 110 | else |
| 111 | { |
| 112 | selected_process = app->processes.find (proc); |
| 113 | } |
| 114 | |
| 115 | std::string *process_name = &selected_process->name; |
| 116 | std::string short_process_name = process_name->substr (0, process_name->find (" -")); |
| 117 | |
| 118 | switch (signal) |
| 119 | { |
| 120 | case SIGKILL: |
| 121 | /*xgettext: primary alert message for killing single process*/ |
| 122 | primary = g_strdup_printf (_("Force Stop %s?"), |
| 123 | short_process_name.c_str ()); |
| 124 | break; |
| 125 | |
| 126 | case SIGTERM: |
| 127 | /*xgettext: primary alert message for ending single process*/ |
| 128 | primary = g_strdup_printf (_("End %s?"), |
| 129 | short_process_name.c_str ()); |
| 130 | break; |
| 131 | |
| 132 | default: // SIGSTOP |
| 133 | /*xgettext: primary alert message for stopping single process*/ |
| 134 | primary = g_strdup_printf (_("Temporarily Stop %s?"), |
| 135 | short_process_name.c_str ()); |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | switch (signal) |
| 142 | { |
| 143 | case SIGKILL: |
| 144 | /*xgettext: primary alert message for killing multiple processes*/ |
| 145 | primary = g_strdup_printf (ngettext ("Force Stop Selected Process?", |
| 146 | "Force Stop %d Selected Processes?", selected_count), |
| 147 | selected_count); |
| 148 | break; |
| 149 | |
| 150 | case SIGTERM: |
| 151 | /*xgettext: primary alert message for ending multiple processes*/ |
| 152 | primary = g_strdup_printf (ngettext ("End Selected Process?", |
| 153 | "End %d Selected Processes?", selected_count), |
| 154 | selected_count); |
| 155 | break; |
| 156 | |
| 157 | default: // SIGSTOP |
| 158 | /*xgettext: primary alert message for stopping multiple processes*/ |
| 159 | primary = g_strdup_printf (ngettext ("Temporarily Stop Selected Process?", |
| 160 | "Temporarily Stop %d Selected Processes?", selected_count), |
| 161 | selected_count); |
| 162 | break; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | switch (signal) |
| 167 | { |
| 168 | case SIGKILL: |
| 169 | /*xgettext: secondary alert message*/ |
| 170 | secondary = _("Force stopping processes can result in data " |
| 171 | "loss, crashes, and system failures"); |
| 172 | button_text = _("_Force Stop"); |
| 173 | break; |
| 174 | |
| 175 | case SIGTERM: |
| 176 | /*xgettext: secondary alert message*/ |
| 177 | secondary = _("Ending processes can result in data loss, " |
| 178 | "crashes, and system failures"); |
| 179 | button_text = _("_End"); |
| 180 | break; |
| 181 | |
| 182 | default: // SIGSTOP |
| 183 | /*xgettext: secondary alert message*/ |
| 184 | secondary = _("Stopping (pausing) processes can result in data loss, " |
| 185 | "crashes, and system failures"); |
| 186 | button_text = _("_Pause"); |
| 187 | break; |
| 188 | } |
| 189 | |
| 190 | kill_alert_dialog = ADW_ALERT_DIALOG (adw_alert_dialog_new (primary, secondary)); |
| 191 | g_free (primary); |
| 192 | |
| 193 | adw_alert_dialog_add_responses (ADW_ALERT_DIALOG (kill_alert_dialog), |
| 194 | "cancel", _("_Cancel"), |
| 195 | "apply", button_text, |
| 196 | NULL); |
| 197 | adw_alert_dialog_set_default_response (ADW_ALERT_DIALOG (kill_alert_dialog), "cancel"); |
| 198 | adw_alert_dialog_set_close_response (ADW_ALERT_DIALOG (kill_alert_dialog), "cancel"); |
| 199 | adw_alert_dialog_set_response_appearance (ADW_ALERT_DIALOG (kill_alert_dialog), "apply", ADW_RESPONSE_DESTRUCTIVE); |
| 200 | |
| 201 | cancellable = g_cancellable_new (); |
| 202 | |
| 203 | adw_alert_dialog_choose (ADW_ALERT_DIALOG (kill_alert_dialog), |
| 204 | GTK_WIDGET (get_active_window ()), |
| 205 | cancellable, |
| 206 | kill_dialog_choose, |
| 207 | kargs); |
| 208 | } |
| 209 | |
| 210 | static void |
| 211 | renice_adjustment_value_changed (GtkAdjustment *adjustment, |
| 212 | gpointer data) |
| 213 | { |
| 214 | GtkLabel *label = GTK_LABEL (data); |
| 215 | |
| 216 | new_nice_value = int(gtk_adjustment_get_value (adjustment)); |
| 217 | |
| 218 | gchar *text = g_strdup (procman::get_nice_level_with_priority (new_nice_value)); |
| 219 | gtk_label_set_text (label, text); |
| 220 | |
| 221 | g_free (text); |
| 222 | } |
| 223 | |
| 224 | static void |
| 225 | renice_dialog_response (AdwAlertDialog*, |
| 226 | gchar *response, |
| 227 | gpointer data) |
| 228 | { |
| 229 | GsmApplication *app = static_cast<GsmApplication *>(data); |
| 230 | |
| 231 | if (g_str_equal (response, "change")) |
| 232 | renice (app, new_nice_value); |
| 233 | |
| 234 | renice_dialog = NULL; |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | procdialog_create_renice_dialog (GsmApplication *app) |
| 239 | { |
| 240 | GtkAdjustment *renice_adjustment; |
| 241 | GtkBuilder *builder; |
| 242 | GtkLabel *priority_label; |
| 243 | ProcInfo *info; |
| 244 | gchar *dialog_title; |
| 245 | |
| 246 | if (renice_dialog) |
| 247 | return; |
| 248 | |
| 249 | gtk_tree_selection_selected_foreach (app->selection, get_last_selected, |
| 250 | &info); |
| 251 | gint selected_count = gtk_tree_selection_count_selected_rows (app->selection); |
| 252 | |
| 253 | if (!info) |
| 254 | return; |
| 255 | |
| 256 | new_nice_value = info->nice; |
| 257 | |
| 258 | builder = gtk_builder_new (); |
| 259 | GError *err = NULL; |
| 260 | |
| 261 | gtk_builder_add_from_resource (builder, "/org/gnome/gnome-system-monitor/data/renice.ui", &err); |
| 262 | if (err != NULL) |
| 263 | g_error ("%s", err->message); |
| 264 | |
| 265 | priority_label = GTK_LABEL (gtk_builder_get_object (builder, "priority_label")); |
| 266 | gtk_label_set_label (priority_label, procman::get_nice_level_with_priority (info->nice)); |
| 267 | |
| 268 | renice_adjustment = GTK_ADJUSTMENT (gtk_builder_get_object (builder, "renice_adjustment")); |
| 269 | gtk_adjustment_configure (GTK_ADJUSTMENT (renice_adjustment), info->nice, RENICE_VAL_MIN, RENICE_VAL_MAX, 1, 1, 0); |
| 270 | g_signal_connect (G_OBJECT (renice_adjustment), |
| 271 | "value_changed", |
| 272 | G_CALLBACK (renice_adjustment_value_changed), |
| 273 | priority_label); |
| 274 | |
| 275 | renice_dialog = ADW_ALERT_DIALOG (gtk_builder_get_object (builder, "renice_dialog")); |
| 276 | |
| 277 | if (selected_count == 1) |
| 278 | dialog_title = g_strdup_printf (_("Change Priority of %s (PID %u)"), |
| 279 | info->name.c_str (), info->pid); |
| 280 | else |
| 281 | dialog_title = g_strdup_printf (ngettext ("Change Priority of the Selected Process", "Change Priority of %d Selected Processes", selected_count), |
| 282 | selected_count); |
| 283 | adw_alert_dialog_set_heading (renice_dialog, dialog_title); |
| 284 | g_free (dialog_title); |
| 285 | |
| 286 | g_signal_connect (G_OBJECT (renice_dialog), |
| 287 | "response", |
| 288 | G_CALLBACK (renice_dialog_response), |
| 289 | app); |
| 290 | |
| 291 | adw_dialog_present (ADW_DIALOG (renice_dialog), GTK_WIDGET (app->main_window)); |
| 292 | |
| 293 | g_object_unref (G_OBJECT (builder)); |
| 294 | } |