1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
|
/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 4 -*-
* -*- coding: utf-8 -*-
*
* Copyright (C) 2011 ~ 2018 Deepin, Inc.
* 2011 ~ 2018 Wang Yong
*
* Author: Wang Yong <wangyong@deepin.com>
* Maintainer: Wang Yong <wangyong@deepin.com>
*
* 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
* 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, see <http://www.gnu.org/licenses/>.
*/
using Gdk;
using Gee;
using Gtk;
using Rsvg;
using Cairo;
extern char* project_path();
extern string font_match(string family);
extern string[] list_mono_or_dot_fonts(out int num);
namespace Utils {
[DBus (name = "com.deepin.Manual.Open")]
interface DeepinManualInterface : Object {
public abstract void ShowManual(string appName) throws GLib.Error;
}
public Gdk.RGBA hex_to_rgba(string hex_color, double alpha=1.0) {
Gdk.RGBA rgba_color = Gdk.RGBA();
rgba_color.parse(hex_color);
rgba_color.alpha = alpha;
return rgba_color;
}
public void set_context_color(Cairo.Context cr, Gdk.RGBA color) {
cr.set_source_rgba(color.red, color.green, color.blue, color.alpha);
}
public void propagate_draw(Gtk.Container widget, Cairo.Context cr) {
if (widget.get_children().length() > 0) {
foreach (Gtk.Widget child in widget.get_children()) {
widget.propagate_draw(child, cr);
}
}
}
public bool is_light_color(string color_string) {
Gdk.RGBA color = Gdk.RGBA();
color.parse(color_string);
double r = color.red;
double g = color.green;
double b = color.blue;
double max_v = double.max(r, double.max(g, b));
double min_v = double.min(r, double.min(g, b));
double s;
if (max_v == 0) {
s = 0.0;
} else {
s = 1.0 - min_v / max_v;
}
b = max_v;
return b > 0.35 && s < 0.7;
}
public double get_color_brightness(string color_string) {
Gdk.RGBA color = Gdk.RGBA();
color.parse(color_string);
double r = color.red;
double g = color.green;
double b = color.blue;
double max_v = double.max(r, double.max(g, b));
double min_v = double.min(r, double.min(g, b));
double s;
if (max_v == 0) {
s = 0.0;
} else {
s = 1.0 - min_v / max_v;
}
b = max_v;
return b;
}
public void touch_dir(string dir) {
var dir_file = GLib.File.new_for_path(dir);
if (!dir_file.query_exists()) {
try {
dir_file.make_directory_with_parents(null);
} catch (GLib.Error err) {
print("Could not create dir: %s\n", err.message);
}
}
}
public void create_file(string file_path) {
var file = GLib.File.new_for_path(file_path);
if (!file.query_exists()) {
try {
file.create(GLib.FileCreateFlags.NONE, null);
} catch (GLib.Error e) {
print("create_file: %s\n", e.message);
}
}
}
public ArrayList<string> list_files(string path) {
ArrayList<string> files = new ArrayList<string>();
try {
FileEnumerator enumerator = File.new_for_path(path).enumerate_children(
"standard::*",
FileQueryInfoFlags.NOFOLLOW_SYMLINKS);
FileInfo info = null;
while (((info = enumerator.next_file()) != null)) {
if (info.get_file_type() != FileType.DIRECTORY) {
files.add(info.get_name());
}
}
} catch (GLib.Error e) {
print("list_files: %s\n", e.message);
}
return files;
}
public void remove_all_children(Gtk.Container container) {
foreach (Widget w in container.get_children()) {
container.remove(w);
}
}
public void destroy_all_children(Gtk.Container container) {
foreach (Widget w in container.get_children()) {
container.remove(w);
w.destroy();
}
}
public Gtk.Allocation get_origin_allocation(Gtk.Widget w) {
Gtk.Allocation alloc;
w.get_allocation(out alloc);
w.translate_coordinates(w.get_toplevel(), 0, 0, out alloc.x, out alloc.y);
return alloc;
}
public bool move_window(Gtk.Widget widget, Gdk.EventButton event, Gtk.Window window) {
if (is_left_button(event)) {
window.begin_move_drag(
(int)event.button,
(int)event.x_root,
(int)event.y_root,
event.time);
}
return false;
}
public void toggle_max_window(Gtk.Window window) {
var window_state = window.get_window().get_state();
if (Gdk.WindowState.MAXIMIZED in window_state) {
window.unmaximize();
} else {
window.maximize();
}
}
public bool is_left_button(Gdk.EventButton event) {
return event.button == 1;
}
public bool is_mouse_wheel(Gdk.EventButton event) {
return event.button == 2;
}
public bool is_right_button(Gdk.EventButton event) {
return event.button == 3;
}
public bool is_action_mouse_button(Gdk.EventButton event) {
return is_left_button(event) || is_mouse_wheel(event);
}
public bool is_double_click(Gdk.EventButton event) {
return event.button == 1 && event.type == Gdk.EventType.2BUTTON_PRESS;
}
public void load_css_theme(string css_path) {
var screen = Gdk.Screen.get_default();
var css_provider = new Gtk.CssProvider();
try {
css_provider.load_from_path(css_path);
} catch (GLib.Error e) {
print("Got error when load css: %s\n", e.message);
}
Gtk.StyleContext.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER);
}
public string slice_string(string str, int unichar_num) {
string slice_str = "";
unichar c;
for (int i = 0; str.get_next_char(ref i, out c);) {
if (i > unichar_num) {
return slice_str.concat("... ");
} else {
slice_str = slice_str.concat(c.to_string());
}
}
return slice_str;
}
public bool is_command_exist(string command_name) {
string? paths = Environment.get_variable("PATH");
foreach (string bin_path in paths.split(":")) {
var file = File.new_for_path(GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, bin_path, command_name));
if (file.query_exists()) {
return true;
}
}
return false;
}
public string get_command_output(string cmd) {
try {
int exit_code;
string std_out;
Process.spawn_command_line_sync(cmd, out std_out, null, out exit_code);
return std_out;
} catch (GLib.Error e){
return "";
}
}
public string get_proc_file_content(string proc_file_path) {
try {
uint8[] data;
GLib.FileUtils.get_data(proc_file_path, out data);
if (data != null) {
for(var i=0; i<data.length-1; i++){
if (data[i]==0) {
data[i]=' ';
}
}
return (string) data;
} else {
return "";
}
} catch (GLib.Error e) {
print("get_proc_file_content: %s\n", e.message);
return "";
}
}
public string get_image_path(string image_name) {
#if TEST_BUILD
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, (string) project_path(), "image", image_name);
#else
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, (string) project_path(), "share", "deepin-terminal", "image", image_name);
#endif
}
public string get_theme_path(string theme_name) {
#if TEST_BUILD
var theme_path = GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, (string) project_path(), "theme", theme_name);
#else
var theme_path = GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, (string) project_path(), "share", "deepin-terminal", "theme", theme_name);
#endif
var dir_file = GLib.File.new_for_path(theme_path);
if (!dir_file.query_exists())
theme_path = get_additional_theme_path(theme_name);
return theme_path;
}
public string get_additional_theme_path(string theme_name) {
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, GLib.Path.get_dirname((string) get_additional_theme_dir()), "themes", theme_name);
}
public string get_theme_dir() {
#if TEST_BUILD
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, (string) project_path(), "theme");
#else
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, (string) project_path(), "share", "deepin-terminal", "theme");
#endif
}
public string get_additional_theme_dir() {
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, GLib.Path.get_dirname((string) get_config_dir()), "deepin-terminal", "themes");
}
public string get_root_path(string file_path) {
#if TEST_BUILD
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, (string) project_path(), file_path);
#else
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, (string) project_path(), "share", "deepin-terminal", file_path);
#endif
}
public string get_config_dir() {
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, Environment.get_user_config_dir(), "deepin", "deepin-terminal");
}
public string get_config_file_path(string config_name) {
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, Environment.get_user_config_dir(), "deepin", "deepin-terminal", config_name);
}
public string get_ssh_script_path() {
#if TEST_BUILD
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, (string) project_path(), "ssh_login.sh");
#else
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, (string) project_path(), "lib", "deepin-terminal", "ssh_login.sh");
#endif
}
public string get_default_private_key_path() {
return GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, Environment.get_home_dir(), ".ssh", "id_rsa");
}
public string lookup_password(string user, string server_address, string? port=null) {
Secret.Schema? password_schema;
if (port == null) {
password_schema = new Secret.Schema("com.deepin.terminal.password.%s.%s".printf(user, server_address),
Secret.SchemaFlags.NONE,
"number", Secret.SchemaAttributeType.INTEGER,
"string", Secret.SchemaAttributeType.STRING,
"even", Secret.SchemaAttributeType.BOOLEAN);
} else {
password_schema = new Secret.Schema("com.deepin.terminal.password.%s.%s.%s".printf(user, server_address, port),
Secret.SchemaFlags.NONE,
"number", Secret.SchemaAttributeType.INTEGER,
"string", Secret.SchemaAttributeType.STRING,
"even", Secret.SchemaAttributeType.BOOLEAN);
}
string password;
try {
password = Secret.password_lookup_sync(password_schema, null, null, "number", 8, "string", "eight", "even", true);
// print("Lookup password: '%s'\n", password);
} catch (GLib.Error e) {
error ("%s", e.message);
}
if (password == null) {
return "";
} else {
return password;
}
}
public void store_password(string user, string server_address, int port, string password) {
var password_schema = new Secret.Schema("com.deepin.terminal.password.%s.%s.%i".printf(user, server_address, port),
Secret.SchemaFlags.NONE,
"number", Secret.SchemaAttributeType.INTEGER,
"string", Secret.SchemaAttributeType.STRING,
"even", Secret.SchemaAttributeType.BOOLEAN);
var attributes = new GLib.HashTable<string,string>(null, null);
attributes["number"] = "8";
attributes["string"] = "eight";
attributes["even"] = "true";
try {
Secret.password_clear_sync(password_schema, null, "number", 8, "string", "eight", "even", true);
} catch (GLib.Error e) {
print("%s", e.message);
return;
}
Secret.password_storev.begin(password_schema, attributes, Secret.COLLECTION_DEFAULT,
"com.deepin.terminal.password.%s.%s".printf(user, server_address),
password,
null, (obj, async_res) => {
try {
Secret.password_store.end(async_res);
} catch (GLib.Error e) {
print("%s", e.message);
return;
}
});
}
public void get_pointer_position(out int x, out int y) {
Gdk.Display gdk_display = Gdk.Display.get_default();
var seat = gdk_display.get_default_seat();
var device = seat.get_pointer();
device.get_position(null, out x, out y);
}
public bool pointer_in_widget_area(Gtk.Widget widget) {
int pointer_x, pointer_y;
Utils.get_pointer_position(out pointer_x, out pointer_y);
Gtk.Allocation widget_rect = get_origin_allocation(widget);
int window_x, window_y;
widget.get_toplevel().get_window().get_root_origin(out window_x, out window_y);
return pointer_x > window_x + widget_rect.x && pointer_x < window_x + widget_rect.x + widget_rect.width && pointer_y > window_y + widget_rect.y && pointer_y < window_y + widget_rect.y + widget_rect.height;
}
public void show_manual() {
try {
DeepinManualInterface deepin_manual_interface = Bus.get_proxy_sync(BusType.SESSION, "com.deepin.Manual.Open", "/com/deepin/Manual/Open");
deepin_manual_interface.ShowManual("deepin-terminal");
} catch (GLib.Error e) {
print("show_manual: %s\n", e.message);
}
}
public void write_log(string log) {
var log_file_dir = GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, Environment.get_user_cache_dir(), "deepin", "deepin-terminal");
var log_file = GLib.Path.build_path(GLib.Path.DIR_SEPARATOR_S, Environment.get_user_cache_dir(), "deepin", "deepin-terminal", "deepin-terminal.log");
touch_dir(log_file_dir);
try {
FileUtils.set_contents(log_file, log);
} catch (GLib.Error e) {
print("write_log: %s\n", e.message);
}
}
public Cairo.ImageSurface create_image_surface(string surface_path) {
try {
var scale = get_default_monitor_scale();
Rsvg.Handle r = new Rsvg.Handle.from_file(Utils.get_image_path(surface_path));
Rsvg.DimensionData d = r.get_dimensions();
Cairo.ImageSurface cs = new Cairo.ImageSurface(Cairo.Format.ARGB32, (int)(d.width * scale), (int)(d.height * scale));
cs.set_device_scale(scale, scale);
Cairo.Context c = new Cairo.Context(cs);
r.render_cairo(c);
return cs;
} catch (GLib.Error e) {
print("create_image_surface: %s %s\n", surface_path, e.message);
Cairo.ImageSurface cs = new Cairo.ImageSurface(Cairo.Format.ARGB32, 1, 1);
return cs;
}
}
public int get_active_monitor(Gdk.Screen screen) {
var window = screen.get_active_window();
if (window != null) {
return screen.get_monitor_at_window(window);
} else {
return screen.get_primary_monitor();
}
}
public double get_default_monitor_scale() {
var display = Display.get_default();
if (display != null) {
var monitor = display.get_primary_monitor();
if (monitor != null) {
return monitor.get_scale_factor();
}
}
return 1.0;
}
public double get_dde_scale_ratio() {
var scaleStr = GLib.Environment.get_variable("QT_SCALE_FACTOR");
if (scaleStr == null) {
return 1.0;
}
var parsedScale = double.parse(scaleStr);
return parsedScale == 0 ? 1.0 : parsedScale;
}
public int get_pointer_monitor(Gdk.Screen screen) {
int x, y;
get_pointer_position(out x, out y);
return screen.get_monitor_at_point(x, y);
}
public string get_process_cmdline(int pid) {
var cmd_file = File.new_for_path ("/proc/%d/cmdline".printf(pid));
string command = "";
if (!cmd_file.query_exists()) {
return command;
}
try {
var dis = new DataInputStream (cmd_file.read ());
uint8[] data = new uint8[4097];
var size = dis.read (data);
if (size <= 0) {
command = "";
}
for (int pos = 0; pos < size; pos++) {
if (data[pos] == '\0' || data[pos] == '\n') {
data[pos] = ' ';
}
}
command = (string) data;
} catch (GLib.Error e) {
warning ("%s\n", e.message);
}
return command;
}
}
|