├── NOTES ├── debian ├── docs ├── compat ├── source │ └── format ├── aptik-battery-monitor.prerm ├── aptik-battery-monitor.postinst ├── rules ├── control ├── changelog ├── aptik-battery-monitor.appdata.xml └── copyright ├── man └── ChangeLog ├── AUTHORS ├── INSTALL ├── images ├── PayPal.png ├── patreon.png ├── charging.png └── discharging.png ├── src ├── share │ ├── aptik-battery-monitor │ │ └── images │ │ │ ├── item-blue.png │ │ │ ├── item-gray.png │ │ │ ├── item-green.png │ │ │ ├── item-red.png │ │ │ ├── item-yellow.png │ │ │ ├── notification-battery-000.png │ │ │ ├── notification-battery-020.png │ │ │ ├── notification-battery-040.png │ │ │ ├── notification-battery-060.png │ │ │ ├── notification-battery-080.png │ │ │ ├── notification-battery-100.png │ │ │ ├── notification-battery-000-plugged.png │ │ │ ├── notification-battery-020-plugged.png │ │ │ ├── notification-battery-040-plugged.png │ │ │ ├── notification-battery-060-plugged.png │ │ │ ├── notification-battery-080-plugged.png │ │ │ ├── notification-battery-100-plugged.png │ │ │ ├── config.svg │ │ │ ├── help-info.svg │ │ │ └── donate.svg │ └── pixmaps │ │ └── aptik-battery-monitor.svg ├── aptik-battery-monitor.desktop ├── aptik-battery-monitor-service ├── Gtk │ ├── SettingsWindow.vala │ ├── AptikBatteryStatsGtk.vala │ ├── DonationWindow.vala │ ├── AboutWindow.vala │ └── BatteryStatsWindow.vala ├── aptik-battery-monitor-uninstall ├── AptikBatteryStats.vala ├── Common │ ├── BatteryCycle.vala │ ├── BatteryStat.vala │ └── Main.vala ├── makefile ├── BatteryBar.vala └── DiskIndicator.vala ├── makefile ├── BUILD_CONFIG ├── .bzrignore ├── .gitignore ├── release ├── sanity.config └── postinst.sh ├── README.md ├── aptik-battery-monitor.pot └── LICENSE.md /NOTES: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /debian/docs: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /man/ChangeLog: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /debian/compat: -------------------------------------------------------------------------------- 1 | 8 2 | -------------------------------------------------------------------------------- /debian/source/format: -------------------------------------------------------------------------------- 1 | 3.0 (native) 2 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | 2 | Copyright (C) 2015 Tony George (teejee2008@gmail.com) 3 | 4 | -------------------------------------------------------------------------------- /INSTALL: -------------------------------------------------------------------------------- 1 | 2 | # build 3 | make all 4 | 5 | # install 6 | sudo make install 7 | -------------------------------------------------------------------------------- /images/PayPal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/images/PayPal.png -------------------------------------------------------------------------------- /images/patreon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/images/patreon.png -------------------------------------------------------------------------------- /images/charging.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/images/charging.png -------------------------------------------------------------------------------- /images/discharging.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/images/discharging.png -------------------------------------------------------------------------------- /debian/aptik-battery-monitor.prerm: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #DEBHELPER# 4 | 5 | set -e 6 | 7 | #stop service 8 | /etc/init.d/aptik-battery-monitor stop 9 | -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/item-blue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/item-blue.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/item-gray.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/item-gray.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/item-green.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/item-green.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/item-red.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/item-red.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/item-yellow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/item-yellow.png -------------------------------------------------------------------------------- /makefile: -------------------------------------------------------------------------------- 1 | all: 2 | cd src; make all 3 | 4 | clean: 5 | cd src; make clean 6 | 7 | install: 8 | cd src; make install 9 | 10 | uninstall: 11 | cd src; make uninstall 12 | -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-000.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-000.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-020.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-020.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-040.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-040.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-060.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-060.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-080.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-080.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-100.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-100.png -------------------------------------------------------------------------------- /BUILD_CONFIG: -------------------------------------------------------------------------------- 1 | 2 | app_fullname="Aptik Battery Monitor" 3 | app_name="aptik-battery-monitor" 4 | pkg_name="aptik-battery-monitor" 5 | pkg_version=$(dpkg-parsechangelog --show-field Version) 6 | git_origin="" 7 | -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-000-plugged.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-000-plugged.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-020-plugged.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-020-plugged.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-040-plugged.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-040-plugged.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-060-plugged.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-060-plugged.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-080-plugged.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-080-plugged.png -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/notification-battery-100-plugged.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/teejee2008/battery-monitor/HEAD/src/share/aptik-battery-monitor/images/notification-battery-100-plugged.png -------------------------------------------------------------------------------- /.bzrignore: -------------------------------------------------------------------------------- 1 | src/aptik-battery-monitor 2 | src/aptik-battery-monitor-gtk 3 | src/aptik-battery-bar 4 | release/source 5 | release/amd64 6 | release/i386 7 | release/armel 8 | release/armhf 9 | release/*.deb 10 | release/*.run 11 | *.geany 12 | .git 13 | .bzr 14 | *.c 15 | *.o 16 | *~ 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | src/aptik-battery-monitor 2 | src/aptik-battery-monitor-gtk 3 | src/aptik-battery-bar 4 | release/source 5 | release/amd64 6 | release/i386 7 | release/armel 8 | release/armhf 9 | release/*.deb 10 | release/*.run 11 | *.geany 12 | .git 13 | .bzr 14 | *.c 15 | *.o 16 | *~ 17 | -------------------------------------------------------------------------------- /release/sanity.config: -------------------------------------------------------------------------------- 1 | app_name: Aptik NG 2 | depends_debian: libgee-0.8-2 libjson-glib-1.0-0 apt aptitude gdebi apt-transport-https pv 3 | depends_redhat: libgee json-glib pv 4 | depends_arch: libgee json-glib pv 5 | depends_generic: libgee json-glib pv 6 | assume_yes: 0 7 | exec_line: pkexec aptik 8 | -------------------------------------------------------------------------------- /debian/aptik-battery-monitor.postinst: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | #DEBHELPER# 4 | 5 | set -e 6 | 7 | #create log directory 8 | mkdir -p /var/log/aptik-battery-monitor 9 | chmod a+rwx /var/log/aptik-battery-monitor 10 | 11 | #start service 12 | /etc/init.d/aptik-battery-monitor start 13 | update-rc.d aptik-battery-monitor defaults 14 | -------------------------------------------------------------------------------- /release/postinst.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | app_name='aptik-battery-monitor' 4 | app_fullname='Aptik Battery Monitor' 5 | 6 | echo "Creating log directory..." 7 | mkdir -p /var/log/${app_name} 8 | chmod a+rwx /var/log/${app_name} 9 | 10 | echo "Starting service..." 11 | /etc/init.d/${app_name} start 12 | update-rc.d ${app_name} defaults 13 | 14 | -------------------------------------------------------------------------------- /src/aptik-battery-monitor.desktop: -------------------------------------------------------------------------------- 1 | [Desktop Entry] 2 | Name=Aptik Battery Monitor 3 | MimeType= 4 | Exec=aptik-battery-monitor-gtk 5 | Type=Application 6 | GenericName=Battery Monitor 7 | Terminal=false 8 | Icon=aptik-battery-monitor 9 | Comment=Battery Monitor 10 | X-KDE-StartupNotify=false 11 | Categories=System; 12 | Name[en_US]=Aptik Battery Monitor 13 | -------------------------------------------------------------------------------- /debian/rules: -------------------------------------------------------------------------------- 1 | #!/usr/bin/make -f 2 | # -*- makefile -*- 3 | # Sample debian/rules that uses debhelper. 4 | # This file was originally written by Joey Hess and Craig Small. 5 | # As a special exception, when this file is copied by dh-make into a 6 | # dh-make output file, you may use that output file without restriction. 7 | # This special exception was added by Craig Small in version 0.37 of dh-make. 8 | 9 | # Uncomment this to turn on verbose mode. 10 | #export DH_VERBOSE=1 11 | 12 | override_dh_usrlocal: 13 | 14 | %: 15 | dh $@ --with autotools-dev 16 | -------------------------------------------------------------------------------- /debian/control: -------------------------------------------------------------------------------- 1 | Source: aptik-battery-monitor 2 | Section: utils 3 | Priority: extra 4 | Maintainer: Tony George 5 | Build-Depends: debhelper (>= 8.0.0), autotools-dev, valac, libgtk-3-dev, libgee-0.8-dev, libjson-glib-dev 6 | Standards-Version: 3.9.3 7 | Homepage: http://teejeetech.blogspot.in/ 8 | #Vcs-Git: git://git.debian.org/collab-maint/hello.git 9 | #Vcs-Browser: http://git.debian.org/?p=collab-maint/hello.git;a=summary 10 | 11 | Package: aptik-battery-monitor 12 | Architecture: any 13 | Depends: ${shlibs:Depends}, ${misc:Depends} 14 | #Recommends: 15 | Description: Utility to monitory battery life on laptops 16 | Graphical utility to monitory battery life on laptops 17 | -------------------------------------------------------------------------------- /debian/changelog: -------------------------------------------------------------------------------- 1 | aptik-battery-monitor (17.12) xenial; urgency=low 2 | 3 | * Display battery health in dashboard 4 | * Updated build scripts 5 | * Added appdata file 6 | * UI improvements 7 | 8 | -- Tony George Sun, 10 Dec 2017 10:00:00 +0530 9 | 10 | aptik-battery-monitor (2.1) trusty; urgency=low 11 | 12 | * Rotate log file when laptop is removed from charger 13 | * Improved readability of console output 14 | * Removed admin access requirement 15 | * Added a service for logging statistics 16 | * Changed logging directory to /var/log/aptik-battery-monitor 17 | * Renamed package, project and binaries from 'aptik-bmon' to 'aptik-battery-monitor' 18 | * Updated UI 19 | 20 | -- Tony George Sat, 12 Dec 2015 14:00:00 +0530 21 | -------------------------------------------------------------------------------- /debian/aptik-battery-monitor.appdata.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | aptik-battery-monitor.desktop 5 | CC-BY-SA-3.0 6 | GPL-3.0 7 | Aptik Battery Monitor 8 | Battery Monitor for laptops 9 | 10 | 11 |

12 | Battery monitor for laptops. Displays detailed statistics for battery life, remaining capacity, and charge/discharge rate. 13 |

14 |
15 | 16 | 17 | 18 | https://raw.githubusercontent.com/teejee2008/battery-monitor/master/images/discharging.png 19 | 20 | 21 | https://raw.githubusercontent.com/teejee2008/battery-monitor/master/images/charging.png 22 | 23 | 24 | 25 | https://github.com/teejee2008/battery-monitor 26 |
27 | 28 | -------------------------------------------------------------------------------- /debian/copyright: -------------------------------------------------------------------------------- 1 | Format: http://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ 2 | Upstream-Name: aptik-battery-monitor 3 | Source: 4 | 5 | Files: * 6 | Copyright: 2015 Tony George 7 | License: GPL-2+ 8 | This package is free software; you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation; either version 2 of the License, or 11 | (at your option) any later version. 12 | . 13 | This package is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | . 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see 20 | . 21 | On Debian systems, the complete text of the GNU General 22 | Public License version 2 can be found in "/usr/share/common-licenses/GPL-2". 23 | -------------------------------------------------------------------------------- /src/aptik-battery-monitor-service: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | ### BEGIN INIT INFO 3 | # Provides: aptik-battery-monitor 4 | # Required-Start: $remote_fs $syslog 5 | # Required-Stop: $remote_fs $syslog 6 | # Default-Start: 2 3 4 5 7 | # Default-Stop: 0 1 6 8 | # Short-Description: start/stop the aptik battery monitor daemon 9 | ### END INIT INFO 10 | # 11 | # Based on skeleton written by Miquel van Smoorenburg . 12 | # Modified for Debian GNU/Linux by Ian Murdock . 13 | # Modified for battery-stats by Karl E. Jørgensen 14 | 15 | PATH=/sbin:/bin:/usr/sbin:/usr/bin 16 | DAEMON=/usr/bin/aptik-battery-monitor 17 | NAME=aptik-battery-monitor 18 | DESC="Aptik Battery Monitor Service" 19 | 20 | test -f $DAEMON || exit 0 21 | 22 | set -e 23 | 24 | case "$1" in 25 | start) 26 | echo -n "Starting $DESC: " 27 | start-stop-daemon --start --background --make-pidfile --quiet \ 28 | --pidfile /var/run/$NAME.pid \ 29 | --exec $DAEMON 30 | echo "$NAME." 31 | ;; 32 | stop) 33 | echo -n "Stopping $DESC: " 34 | start-stop-daemon --oknodo --stop --quiet --pidfile /var/run/$NAME.pid \ 35 | --exec $DAEMON 36 | echo "$NAME." 37 | ;; 38 | reload|restart|force-reload) 39 | # 40 | # If the "reload" option is implemented, move the "force-reload" 41 | # option to the "reload" entry above. If not, "force-reload" is 42 | # just the same as "restart". 43 | # 44 | echo -n "Restarting $DESC: " 45 | start-stop-daemon --stop --quiet --pidfile \ 46 | /var/run/$NAME.pid --exec $DAEMON 47 | sleep 1 48 | start-stop-daemon --background --make-pidfile --start --quiet \ 49 | --pidfile /var/run/$NAME.pid \ 50 | --exec $DAEMON -- $SYS_ARGS $ARGS 51 | echo "$NAME." 52 | ;; 53 | *) 54 | N=/etc/init.d/$NAME 55 | # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2 56 | echo "Usage: $N {start|stop|restart|force-reload}" >&2 57 | exit 1 58 | ;; 59 | esac 60 | 61 | exit 0 62 | -------------------------------------------------------------------------------- /src/Gtk/SettingsWindow.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * SettingsWindow.vala 3 | * 4 | * Copyright 2012-2017 Tony George 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301, USA. 20 | * 21 | * 22 | */ 23 | 24 | 25 | using Gtk; 26 | using Gee; 27 | 28 | using TeeJee.Logging; 29 | using TeeJee.FileSystem; 30 | using TeeJee.JSON; 31 | using TeeJee.ProcessManagement; 32 | using TeeJee.GtkHelper; 33 | using TeeJee.Multimedia; 34 | using TeeJee.System; 35 | using TeeJee.Misc; 36 | 37 | public class SettingsWindow : Dialog { 38 | 39 | private Gtk.Switch switch_show_bar; 40 | 41 | public SettingsWindow() { 42 | //set_size_request(500, 300); 43 | set_title(_("Settings")); 44 | window_position = WindowPosition.CENTER_ON_PARENT; 45 | set_destroy_with_parent (true); 46 | set_modal (true); 47 | set_deletable(true); 48 | set_skip_taskbar_hint(true); 49 | icon = get_app_icon(16); 50 | 51 | //vbox_main 52 | Box vbox_main = get_content_area(); 53 | vbox_main.margin = 6; 54 | vbox_main.homogeneous = false; 55 | 56 | var hbox_show_bar = new Box (Orientation.HORIZONTAL, 6); 57 | hbox_show_bar.margin = 6; 58 | vbox_main.add (hbox_show_bar); 59 | 60 | //lbl_message 61 | Label lbl_bar_enable = new Gtk.Label(_("Show battery bar")); 62 | hbox_show_bar.add(lbl_bar_enable); 63 | 64 | //switch_show_bar 65 | switch_show_bar = new Gtk.Switch(); 66 | switch_show_bar.halign = Align.END; 67 | //switch_show_bar.active = App.is_battery_bar_enabled(); 68 | hbox_show_bar.add(switch_show_bar); 69 | 70 | switch_show_bar.notify["active"].connect(() => { 71 | //App.set_battery_bar_status_cron(switch_show_bar.active); 72 | }); 73 | 74 | var hbox_action = (Box) get_action_area(); 75 | 76 | //btn_close 77 | var btn_close = new Button.with_label(" " + _("OK")); 78 | btn_close.set_image (new Image.from_stock ("gtk-ok", IconSize.MENU)); 79 | hbox_action.add(btn_close); 80 | 81 | btn_close.clicked.connect(()=>{ this.destroy(); }); 82 | } 83 | } 84 | 85 | 86 | -------------------------------------------------------------------------------- /src/aptik-battery-monitor-uninstall: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | app_name='aptik-battery-monitor' 4 | app_fullname='Aptik Battery Monitor' 5 | 6 | Reset='\e[0m' 7 | Red='\e[1;31m' 8 | Green='\e[1;32m' 9 | Yellow='\e[1;33m' 10 | 11 | CHECK_COLOR_SUPPORT() { 12 | colors=`tput colors` 13 | if [ $colors -gt 1 ]; then 14 | COLORS_SUPPORTED=0 15 | else 16 | COLORS_SUPPORTED=1 17 | fi 18 | } 19 | 20 | MSG_INFO() { 21 | add_newline='' 22 | if [ "$2" == 0 ]; then 23 | add_newline='-n' 24 | fi 25 | 26 | if [ $COLORS_SUPPORTED -eq 0 ]; then 27 | echo -e ${add_newline} "[${Yellow}*${Reset}] ${Green}$1${Reset}" 28 | else 29 | echo -e ${add_newline} "[*] $1" 30 | fi 31 | } 32 | 33 | MSG_WARNING() { 34 | add_newline='' 35 | if [ "$2" == 0 ]; then 36 | add_newline='-n' 37 | fi 38 | 39 | if [ $COLORS_SUPPORTED -eq 0 ]; then 40 | echo -e ${add_newline} "[${Red}!${Reset}] ${Yellow}$1${Reset}" 41 | else 42 | echo -e ${add_newline} "[!] $1" 43 | fi 44 | } 45 | 46 | MSG_ERROR() { 47 | add_newline='' 48 | if [ "$2" == 0 ]; then 49 | add_newline='-n' 50 | fi 51 | 52 | if [ $COLORS_SUPPORTED -eq 0 ]; then 53 | echo -e ${add_newline} "[${Red}X${Reset}] ${Yellow}$1${Reset}" 54 | else 55 | echo -e ${add_newline} "[X] $1" 56 | fi 57 | } 58 | 59 | CD_PUSH() { 60 | cd_backup=`pwd` 61 | } 62 | 63 | CD_POP() { 64 | if [ ! -z "${cd_backup}" ]; then 65 | cd "${cd_backup}" 66 | fi 67 | } 68 | 69 | EXIT(){ 70 | CD_POP 71 | exit $1 72 | } 73 | 74 | WAIT_FOR_INPUT() { 75 | echo "" 76 | echo "Press any key to exit..." 77 | read dummy 78 | } 79 | 80 | GET_SCRIPT_PATH(){ 81 | SCRIPTPATH="$(cd "$(dirname "$0")" && pwd)" 82 | SCRIPTNAME=`basename $0` 83 | } 84 | 85 | RUN_AS_ADMIN() { 86 | if [ ! `id -u` -eq 0 ]; then 87 | GET_SCRIPT_PATH 88 | if command -v sudo >/dev/null 2>&1; then 89 | sudo "${SCRIPTPATH}/${SCRIPTNAME}" 90 | EXIT $? 91 | elif command -v su >/dev/null 2>&1; then 92 | su -c "${SCRIPTPATH}/${SCRIPTNAME}" 93 | EXIT $? 94 | else 95 | echo "" 96 | MSG_ERROR "** Uninstaller must be run as Admin (using 'sudo' or 'su') **" 97 | echo "" 98 | EXIT 1 99 | fi 100 | fi 101 | } 102 | 103 | CD_PUSH 104 | CHECK_COLOR_SUPPORT 105 | RUN_AS_ADMIN 106 | 107 | /etc/init.d/${app_name} stop 108 | 109 | if [ ! -z "${app_name}" ]; then 110 | rm -f "/usr/bin/${app_name}" 111 | rm -f "/usr/bin/${app_name}-gtk" 112 | rm -f "/usr/bin/${app_name}-launcher" 113 | rm -f "/usr/bin/${app_name}-uninstall" 114 | rm -f "/usr/share/applications/${app_name}.desktop" 115 | rm -f "/usr/share/pixmaps/${app_name}.svg" 116 | rm -rf "/usr/share/${app_name}" 117 | rm -rf "/usr/share/doc/${app_name}" 118 | rm -f /usr/share/locale/*/LC_MESSAGES/${app_name}.mo 119 | fi 120 | 121 | if [ $? -eq 0 ]; then 122 | echo "${app_fullname} was uninstalled successfully." 123 | EXIT 0 124 | else 125 | echo "Uninstalled completed (some files could not be removed)" 126 | EXIT 1 127 | fi 128 | 129 | CD_POP 130 | -------------------------------------------------------------------------------- /src/Gtk/AptikBatteryStatsGtk.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * AptikBatteryStatsGtk.vala 3 | * 4 | * Copyright 2012-2017 Tony George 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301, USA. 20 | * 21 | * 22 | */ 23 | 24 | using GLib; 25 | using Gtk; 26 | using Gee; 27 | using Json; 28 | 29 | using TeeJee.Logging; 30 | using TeeJee.FileSystem; 31 | using TeeJee.JSON; 32 | using TeeJee.ProcessManagement; 33 | using TeeJee.GtkHelper; 34 | using TeeJee.Multimedia; 35 | using TeeJee.System; 36 | using TeeJee.Misc; 37 | 38 | public class AptikBatteryStatsGtk : GLib.Object{ 39 | 40 | public static int main (string[] args) { 41 | set_locale(); 42 | 43 | Gtk.init(ref args); 44 | 45 | App = new Main(args, true); 46 | parse_arguments(args); 47 | 48 | var window = new BatteryStatsWindow (); 49 | window.destroy.connect(Gtk.main_quit); 50 | window.show_all(); 51 | 52 | //start event loop 53 | Gtk.main(); 54 | 55 | App.exit_app(); 56 | 57 | return 0; 58 | } 59 | 60 | private static void set_locale(){ 61 | Intl.setlocale(GLib.LocaleCategory.MESSAGES, AppShortName); 62 | Intl.textdomain(GETTEXT_PACKAGE); 63 | Intl.bind_textdomain_codeset(GETTEXT_PACKAGE, "utf-8"); 64 | Intl.bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR); 65 | } 66 | 67 | public static bool parse_arguments(string[] args){ 68 | //parse options 69 | for (int k = 1; k < args.length; k++) // Oth arg is app path 70 | { 71 | switch (args[k].down()){ 72 | case "--debug": 73 | LOG_DEBUG = true; 74 | break; 75 | case "--help": 76 | case "--h": 77 | case "-h": 78 | log_msg(help_message()); 79 | exit(0); 80 | return true; 81 | default: 82 | //unknown option - show help and exit 83 | log_error(_("Unknown option") + ": %s".printf(args[k])); 84 | log_msg(help_message()); 85 | exit(1); 86 | return false; 87 | } 88 | } 89 | 90 | return true; 91 | } 92 | 93 | public static string help_message(){ 94 | string msg = "\n" + AppName + " v" + AppVersion + " by Tony George (teejee2008@gmail.com)" + "\n"; 95 | msg += "\n"; 96 | msg += _("Syntax") + ": %s [options]\n".printf(AppShortName); 97 | msg += "\n"; 98 | msg += _("Options") + ":\n"; 99 | msg += "\n"; 100 | msg += " --debug " + _("Print debug information") + "\n"; 101 | msg += " --h[elp] " + _("Show all options") + "\n"; 102 | msg += "\n"; 103 | return msg; 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Battery Monitor 2 | 3 | Battery Monitor is a utility to monitor the battery life of laptops. It displays a graph of the change in battery level with time. This is useful to estimate the amount of battery life that you are getting from your laptop, and to check the overall health. 4 | 5 | **Average Life** is the amount of time your laptop takes to go from 100% to 0%. It takes a few charge-discharge cycles to get an accurate estimate of this value. 6 | 7 | **Health** is the remaining life of battery. It is normally 100% when the battery is new, and slowly degrades with time. For example, a two-year old laptop may show health of 80%, which indicates that only 80% of the original capacity is left. 8 | 9 | *Discharging - Displays average life, time remaining and discharge rate* 10 | 11 | ![](images/discharging.png) 12 | 13 | 14 | 15 | **Black line** in the graph displays battery level at a particular time. 16 | 17 | **Red line** in the graph displays CPU usage. CPU usage is usually high if battery is discharging rapidly. 18 | 19 | *Charging - Displays charge rate and time remaining* 20 | 21 | ![](images/charging.png) 22 | 23 | ## Installation 24 | 25 | #### Ubuntu-based Distributions 26 | 27 | Ubuntu, Linux Mint, Elementary OS, etc. 28 | 29 | Packages are available in the Launchpad PPA for current Ubuntu releases. 30 | Run following commands in a terminal window to install from PPA: 31 | 32 | ```sh 33 | sudo add-apt-repository -y ppa:teejee2008/ppa 34 | sudo apt-get update 35 | sudo apt-get install aptik-battery-monitor 36 | ``` 37 | 38 | Debian packages are available on the [Releases](https://github.com/teejee2008/battery-monitor/releases) page for older Ubuntu releases which have reached end-of-life. 39 | 40 | #### Other Linux Distributions 41 | 42 | Binary installers are available on the [Releases](https://github.com/teejee2008/battery-monitor/releases) page. Download the 32-bit or 64-bit installer and execute from terminal window: 43 | 44 | ```sh 45 | sudo sh ./aptik-battery-monitor*amd64.run # 64-bit, or 46 | sudo sh ./aptik-battery-monitor*i386.run # 32-bit 47 | ``` 48 | 49 | Installer can be used on following distribution types: 50 | 51 | - **Fedora** based - Fedora, RedHat, Cent OS, etc (supports **dnf** and **yum**) 52 | 53 | - **Debian** based - Debian, Ubuntu, Linux Mint, Elementary OS, etc (supports **apt**) 54 | 55 | - **Arch** based - Arch Linux, Manjaro, etc (supports **pacman**) 56 | 57 | ​ 58 | 59 | ## Donate 60 | 61 | *Battery Monitor* is a non-commercial application. I work on it during my free time based on my requirements and interest. 62 | 63 | **PayPal** ~ If you find this application useful and wish to say thanks, you can buy me a coffee by making a donation with Paypal. Your contributions will help keep the project alive and support future development. 64 | 65 | [![](images/PayPal.png)](https://www.paypal.com/cgi-bin/webscr?business=teejeetech@gmail.com&cmd=_xclick¤cy_code=USD&amount=10&item_name=BatteryMonitor%20Donation) 66 | 67 | **Patreon** ~ You can also sign up as a sponsor on [Patreon.com](https://www.patreon.com/teejeetech). As a patron you will get access to beta releases of new applications that I'm working on. You will also get news and updates about new features that are not published elsewhere. 68 | 69 | [![](images/patreon.png)](https://www.patreon.com/bePatron?u=3059450) 70 | 71 | -------------------------------------------------------------------------------- /src/AptikBatteryStats.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * AptikBatteryStats.vala 3 | * 4 | * Copyright 2012-2017 Tony George 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301, USA. 20 | * 21 | * 22 | */ 23 | 24 | using GLib; 25 | using Gtk; 26 | using Gee; 27 | using Soup; 28 | using Json; 29 | 30 | using TeeJee.Logging; 31 | using TeeJee.FileSystem; 32 | using TeeJee.JSON; 33 | using TeeJee.ProcessManagement; 34 | using TeeJee.GtkHelper; 35 | using TeeJee.Multimedia; 36 | using TeeJee.System; 37 | using TeeJee.Misc; 38 | 39 | public class AptikBatteryStats : GLib.Object{ 40 | public static int main (string[] args) { 41 | set_locale(); 42 | 43 | LOG_TIMESTAMP = false; 44 | 45 | App = new Main(args,false); 46 | App.check_for_multiple_instances(); 47 | 48 | var console = new AptikBatteryStats(); 49 | console.parse_arguments(args); 50 | 51 | if (App.print_stats){ 52 | stdout.printf(_("Logging stats to file") + ": '%s'\n\n".printf(Main.BATT_STATS_CACHE_FILE)); 53 | } 54 | 55 | App.read_battery_stats(); 56 | 57 | if (App.command == "print_log"){ 58 | App.print_log_file(); 59 | exit(0); 60 | } 61 | else{ 62 | while(true){ 63 | App.log_battery_stats(App.print_stats); 64 | sleep(Main.BATT_STATS_LOG_INTERVAL * 1000); 65 | } 66 | } 67 | 68 | return 0; 69 | } 70 | 71 | private static void set_locale(){ 72 | Intl.setlocale(GLib.LocaleCategory.MESSAGES, AppShortName); 73 | Intl.textdomain(GETTEXT_PACKAGE); 74 | Intl.bind_textdomain_codeset(GETTEXT_PACKAGE, "utf-8"); 75 | Intl.bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR); 76 | } 77 | 78 | public string help_message(){ 79 | string msg = "\n" + AppName + " v" + AppVersion + " by Tony George (teejee2008@gmail.com)" + "\n"; 80 | msg += "\n"; 81 | msg += _("Syntax") + ": " + AppShortName + " [options]\n"; 82 | msg += "\n"; 83 | msg += _("Options") + ":\n"; 84 | msg += "\n"; 85 | msg += " --print " + _("Print stats to standard output") + "\n"; 86 | msg += " --print-log " + _("Print stats from log file sand quit") + "\n"; 87 | msg += " --h[elp] " + _("Show all options") + "\n"; 88 | msg += "\n"; 89 | return msg; 90 | } 91 | 92 | public bool parse_arguments(string[] args){ 93 | 94 | //parse options 95 | for (int k = 1; k < args.length; k++) // Oth arg is app path 96 | { 97 | switch (args[k].down()){ 98 | case "--debug": 99 | LOG_DEBUG = true; 100 | break; 101 | case "--print": 102 | App.print_stats = true; 103 | break; 104 | case "--print-log": 105 | App.command = "print_log"; 106 | break; 107 | case "--help": 108 | case "--h": 109 | case "-h": 110 | log_msg(help_message()); 111 | return true; 112 | default: 113 | //unknown option - ignore, no errors 114 | return false; 115 | } 116 | } 117 | 118 | return true; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /aptik-battery-monitor.pot: -------------------------------------------------------------------------------- 1 | # SOME DESCRIPTIVE TITLE. 2 | # Copyright (C) YEAR Tony George (teejee2008@gmail.com) 3 | # This file is distributed under the same license as the aptik-battery-monitor package. 4 | # FIRST AUTHOR , YEAR. 5 | # 6 | #, fuzzy 7 | msgid "" 8 | msgstr "" 9 | "Project-Id-Version: aptik-battery-monitor 1.6\n" 10 | "Report-Msgid-Bugs-To: teejee2008@gmail.com\n" 11 | "POT-Creation-Date: 2017-12-10 18:57+0530\n" 12 | "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" 13 | "Last-Translator: FULL NAME \n" 14 | "Language-Team: LANGUAGE \n" 15 | "Language: \n" 16 | "MIME-Version: 1.0\n" 17 | "Content-Type: text/plain; charset=CHARSET\n" 18 | "Content-Transfer-Encoding: 8bit\n" 19 | 20 | #: DiskIndicator.vala:211 21 | msgid "About" 22 | msgstr "" 23 | 24 | #: DiskIndicator.vala:871 25 | msgid "Disk Indicator for Linux" 26 | msgstr "" 27 | 28 | #: DiskIndicator.vala:470 29 | #, c-format 30 | msgid "Disk can be safely removed" 31 | msgstr "" 32 | 33 | #: DiskIndicator.vala:220 34 | msgid "Donate" 35 | msgstr "" 36 | 37 | #: DiskIndicator.vala:774 38 | msgid "Encrypted Device" 39 | msgstr "" 40 | 41 | #: DiskIndicator.vala:775 42 | #, c-format 43 | msgid "Enter passphrase to unlock '%s'" 44 | msgstr "" 45 | 46 | #: DiskIndicator.vala:229 47 | msgid "Exit" 48 | msgstr "" 49 | 50 | #: DiskIndicator.vala:645 51 | msgid "Failed to lock" 52 | msgstr "" 53 | 54 | #: DiskIndicator.vala:669 55 | msgid "Failed to unlock" 56 | msgstr "" 57 | 58 | #: DiskIndicator.vala:174 59 | msgid "ISO" 60 | msgstr "" 61 | 62 | #: DiskIndicator.vala:538 63 | #, c-format 64 | msgid "Is Mounted" 65 | msgstr "" 66 | 67 | #: DiskIndicator.vala:119 68 | msgid "Lock" 69 | msgstr "" 70 | 71 | #: DiskIndicator.vala:640 72 | #, c-format 73 | msgid "Locked" 74 | msgstr "" 75 | 76 | #: AptikBatteryStats.vala:52 77 | #, c-format 78 | msgid "Logging stats to file" 79 | msgstr "" 80 | 81 | #: DiskIndicator.vala:82 82 | msgid "Mount" 83 | msgstr "" 84 | 85 | #: DiskIndicator.vala:694 86 | msgid "Mount ISO..." 87 | msgstr "" 88 | 89 | #: DiskIndicator.vala:531 90 | #, c-format 91 | msgid "Mounted" 92 | msgstr "" 93 | 94 | #: DiskIndicator.vala:722 95 | #, c-format 96 | msgid "Mounted ISO File" 97 | msgstr "" 98 | 99 | #: DiskIndicator.vala:587 100 | #, c-format 101 | msgid "Not mounted" 102 | msgstr "" 103 | 104 | #: DiskIndicator.vala:47 105 | msgid "Open" 106 | msgstr "" 107 | 108 | #: AptikBatteryStats.vala:83 109 | msgid "Options" 110 | msgstr "" 111 | 112 | #: AptikBatteryStats.vala:86 113 | msgid "Print stats from log file sand quit" 114 | msgstr "" 115 | 116 | #: AptikBatteryStats.vala:85 117 | msgid "Print stats to standard output" 118 | msgstr "" 119 | 120 | #: DiskIndicator.vala:193 121 | msgid "Settings" 122 | msgstr "" 123 | 124 | #: AptikBatteryStats.vala:87 125 | msgid "Show all options" 126 | msgstr "" 127 | 128 | #: DiskIndicator.vala:481 129 | msgid "Swap Device!" 130 | msgstr "" 131 | 132 | #: DiskIndicator.vala:482 133 | msgid "Swap devices cannot be opened in file manager" 134 | msgstr "" 135 | 136 | #: AptikBatteryStats.vala:81 137 | msgid "Syntax" 138 | msgstr "" 139 | 140 | #: DiskIndicator.vala:568 DiskIndicator.vala:617 141 | msgid "System Device!" 142 | msgstr "" 143 | 144 | #: DiskIndicator.vala:569 DiskIndicator.vala:618 145 | msgid "System devices cannot be unmounted" 146 | msgstr "" 147 | 148 | #: DiskIndicator.vala:136 149 | msgid "Unlock" 150 | msgstr "" 151 | 152 | #: DiskIndicator.vala:664 153 | #, c-format 154 | msgid "Unlocked" 155 | msgstr "" 156 | 157 | #: DiskIndicator.vala:99 158 | msgid "Unmount" 159 | msgstr "" 160 | 161 | #: DiskIndicator.vala:469 DiskIndicator.vala:581 162 | #, c-format 163 | msgid "Unmounted" 164 | msgstr "" 165 | 166 | #: DiskIndicator.vala:155 167 | msgid "Usage" 168 | msgstr "" 169 | -------------------------------------------------------------------------------- /src/Common/BatteryCycle.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * BatteryCycle.vala 3 | * 4 | * Copyright 2012-2017 Tony George 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301, USA. 20 | * 21 | * 22 | */ 23 | 24 | using GLib; 25 | using Gtk; 26 | using Gee; 27 | using Json; 28 | 29 | using TeeJee.Logging; 30 | using TeeJee.FileSystem; 31 | using TeeJee.JSON; 32 | using TeeJee.ProcessManagement; 33 | using TeeJee.GtkHelper; 34 | using TeeJee.Multimedia; 35 | using TeeJee.System; 36 | using TeeJee.Misc; 37 | 38 | public class BatteryCycle : GLib.Object{ 39 | 40 | public DateTime date; 41 | public double total_drop = 0.0; 42 | public double total_mins = 0.0; 43 | public double drop_per_min = 0.0; 44 | public double average_battery_life_in_mins = 0.0; 45 | public double remaining_mins = 0.0; 46 | 47 | public BatteryCycle(){ 48 | 49 | } 50 | 51 | public void calculate_stats(Gee.ArrayList list){ 52 | BatteryStat stat_last = null; 53 | total_drop = 0.0; 54 | total_mins = 0.0; 55 | drop_per_min = 0.0; 56 | foreach(BatteryStat stat in list){ 57 | if ((stat_last != null) 58 | && (stat.charge_percent() < stat_last.charge_percent()) 59 | && (stat_last.date.add_seconds(Main.BATT_STATS_LOG_INTERVAL + 1).compare(stat.date) > 0)) 60 | { 61 | total_drop += (stat_last.charge_percent() - stat.charge_percent()); 62 | total_mins += 0.5; 63 | } 64 | stat_last = stat; 65 | 66 | } 67 | 68 | if (stat_last != null){ 69 | date = stat_last.date; 70 | } 71 | 72 | if (total_mins > 0){ 73 | drop_per_min = (total_drop / total_mins); 74 | } 75 | 76 | if (drop_per_min > 0){ 77 | average_battery_life_in_mins = (100.0 / drop_per_min); 78 | 79 | if (stat_last != null){ 80 | remaining_mins = (stat_last.charge_percent() / drop_per_min); 81 | } 82 | } 83 | } 84 | 85 | public string to_delimited_string(){ 86 | var txt = date.to_utc().to_unix().to_string() + "|"; 87 | txt += "%.0f|".printf(total_drop * 1000); 88 | txt += "%.0f\n".printf(total_mins * 1000); 89 | return txt; 90 | } 91 | 92 | public string to_friendly_string(){ 93 | var txt = ""; 94 | txt += date.format("%F %H:%M:%S"); 95 | txt += ", Used %0.2f %% in %.0fh %.0fm @ %0.1f %% per hour".printf( 96 | total_drop, 97 | (total_mins / 60.0), 98 | (total_mins % 60.0), 99 | (drop_per_min * 60.0) 100 | ); 101 | txt += "\n"; 102 | return txt; 103 | } 104 | 105 | public string used_string(){ 106 | return "%0.2f %% in %.0fh %.0fm @ %0.1f %% per hour".printf( 107 | total_drop, 108 | (total_mins / 60.0), 109 | (total_mins % 60.0), 110 | (drop_per_min * 60.0) 111 | ); 112 | } 113 | 114 | public string battery_life_string(){ 115 | return "%.0fh %.0fm".printf(average_battery_life_in_mins / 60, average_battery_life_in_mins % 60); 116 | } 117 | 118 | public string remaining_time_string(){ 119 | return "%.0fh %.0fm".printf(remaining_mins / 60, remaining_mins % 60); 120 | } 121 | 122 | public static string mins_to_string(double mins){ 123 | return "%.0fh %.0fm".printf(mins / 60, mins % 60); 124 | } 125 | } 126 | 127 | 128 | -------------------------------------------------------------------------------- /src/Gtk/DonationWindow.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * DonationWindow.vala 3 | * 4 | * Copyright 2012 Tony George 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301, USA. 20 | * 21 | * 22 | */ 23 | 24 | using Gtk; 25 | 26 | using TeeJee.Logging; 27 | using TeeJee.FileSystem; 28 | using TeeJee.JSON; 29 | using TeeJee.ProcessManagement; 30 | using TeeJee.GtkHelper; 31 | using TeeJee.System; 32 | using TeeJee.Misc; 33 | 34 | public class DonationWindow : Dialog { 35 | 36 | public DonationWindow() { 37 | 38 | set_title(_("Donate")); 39 | window_position = WindowPosition.CENTER_ON_PARENT; 40 | set_destroy_with_parent (true); 41 | set_modal (true); 42 | set_deletable(true); 43 | set_skip_taskbar_hint(false); 44 | set_default_size(400, 20); 45 | icon = get_app_icon(16); 46 | 47 | //vbox_main 48 | var vbox_main = get_content_area(); 49 | vbox_main.margin = 6; 50 | vbox_main.margin_right = 12; 51 | vbox_main.homogeneous = false; 52 | 53 | get_action_area().visible = false; 54 | 55 | //lbl_message 56 | var lbl_message = new Gtk.Label(""); 57 | string msg = _("Did you find this application useful?\n\nYou can buy me a coffee if you wish to say thanks, by making a donation with Paypal. Your contributions will help keep this project alive and support future development.\n\n~ Tony George (teejeetech@gmail.com)"); 58 | lbl_message.label = msg; 59 | lbl_message.wrap = true; 60 | vbox_main.pack_start(lbl_message,true,true,0); 61 | 62 | //vbox_actions 63 | var vbox_actions = new Box (Orientation.VERTICAL, 6); 64 | vbox_actions.margin_left = 50; 65 | vbox_actions.margin_right = 50; 66 | vbox_actions.margin_top = 20; 67 | vbox_actions.margin_bottom = 10; 68 | vbox_main.pack_start(vbox_actions,false,false,0); 69 | 70 | // donate_paypal ------------------------------ 71 | 72 | var button = new Button.with_label(_("Donate with PayPal")); 73 | vbox_actions.add(button); 74 | 75 | button.clicked.connect(()=>{ 76 | xdg_open("https://www.paypal.com/cgi-bin/webscr?business=teejeetech@gmail.com&cmd=_xclick¤cy_code=USD&amount=10&item_name=AptikBatteryMonitor%20Donation"); 77 | }); 78 | 79 | // home page ------------------------- 80 | 81 | button = new Button.with_label(_("Project Home Page")); 82 | button.set_tooltip_text("https://github.com/teejee2008/battery-monitor"); 83 | vbox_actions.add(button); 84 | 85 | button.clicked.connect(()=>{ 86 | xdg_open("https://github.com/teejee2008/battery-monitor"); 87 | }); 88 | 89 | // issue tracker -------------------------- 90 | 91 | button = new Button.with_label(_("Issue Tracker")); 92 | button.set_tooltip_text(_("Issue Tracker ~ Report Issues, Request Features, Ask Questions")); 93 | vbox_actions.add(button); 94 | 95 | button.clicked.connect(()=>{ 96 | xdg_open("https://github.com/teejee2008/battery-monitor/issues"); 97 | }); 98 | 99 | // close ------------------------- 100 | 101 | button = new Button.with_label(_("OK")); 102 | vbox_actions.add(button); 103 | 104 | button.clicked.connect(() => { 105 | this.destroy(); 106 | }); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/makefile: -------------------------------------------------------------------------------- 1 | prefix=/usr 2 | bindir=$(prefix)/bin 3 | logdir=/var/log 4 | sharedir=$(prefix)/share 5 | localedir=$(sharedir)/locale 6 | launcherdir=$(sharedir)/applications 7 | mandir=$(sharedir)/man 8 | man1dir=$(mandir)/man1 9 | CFLAGS=--std=c99 10 | app_name=aptik-battery-monitor 11 | app_name_full='Aptik Battery Monitor' 12 | 13 | all: 14 | # build binaries 15 | 16 | # aptik-battery-monitor 17 | valac -X -D'GETTEXT_PACKAGE="${app_name}"' --Xcc="-lm" --thread \ 18 | Common/*.vala "AptikBatteryStats.vala" \ 19 | -o "aptik-battery-monitor" \ 20 | --pkg glib-2.0 --pkg gio-unix-2.0 --pkg posix --pkg gee-0.8 \ 21 | --pkg gtk+-3.0 --pkg libsoup-2.4 --pkg json-glib-1.0 22 | 23 | # aptik-battery-monitor-gtk 24 | valac -X -D'GETTEXT_PACKAGE="${app_name}"' --Xcc="-lm" --thread \ 25 | Common/*.vala Gtk/*.vala \ 26 | -o "aptik-battery-monitor-gtk" \ 27 | --pkg glib-2.0 --pkg gio-unix-2.0 --pkg posix --pkg gee-0.8 \ 28 | --pkg gtk+-3.0 --pkg libsoup-2.4 --pkg json-glib-1.0 29 | 30 | # update translation template 31 | xgettext --language=C --keyword=_ --copyright-holder='Tony George (teejee2008@gmail.com)' --package-name='${app_name}' --package-version='1.6' --msgid-bugs-address='teejee2008@gmail.com' --escape --sort-output -o ../${app_name}.pot *.vala 32 | 33 | clean: 34 | rm -rfv ../release/{source,i386,amd64,armel,armhf} 35 | rm -rfv ../release/*.{run,deb} 36 | rm -rfv *.c *.o *.mo 37 | rm -fv ${app_name} ${app_name}-gtk 38 | 39 | install: 40 | mkdir -p "$(DESTDIR)$(bindir)" 41 | mkdir -p "$(DESTDIR)$(sharedir)" 42 | mkdir -p "$(DESTDIR)$(mandir)" 43 | mkdir -p "$(DESTDIR)$(man1dir)" 44 | mkdir -p "$(DESTDIR)$(launcherdir)" 45 | mkdir -p "$(DESTDIR)$(sharedir)/${app_name}" 46 | mkdir -p "$(DESTDIR)$(sharedir)/pixmaps" 47 | mkdir -p "$(DESTDIR)$(sharedir)/appdata" 48 | mkdir -p "$(DESTDIR)$(logdir)/aptik-battery-monitor" 49 | mkdir -p "$(DESTDIR)/etc/init.d" 50 | #mkdir -p "$(DESTDIR)$(localedir)/it_IT/LC_MESSAGES" 51 | #mkdir -p "$(DESTDIR)$(localedir)/ru_RU/LC_MESSAGES" 52 | #mkdir -p "$(DESTDIR)$(localedir)/ko_KO/LC_MESSAGES" 53 | #mkdir -p "$(DESTDIR)$(localedir)/pt_BR/LC_MESSAGES" 54 | 55 | #binary 56 | install -m 0755 ${app_name} "$(DESTDIR)$(bindir)" 57 | install -m 0755 ${app_name}-gtk "$(DESTDIR)$(bindir)" 58 | install -m 0755 ${app_name}-uninstall "$(DESTDIR)$(bindir)" 59 | 60 | #shared files 61 | cp -dpr --no-preserve=ownership -t "$(DESTDIR)$(sharedir)/${app_name}" ./share/${app_name}/* 62 | chmod --recursive 0755 $(DESTDIR)$(sharedir)/${app_name}/* 63 | 64 | #launcher 65 | install -m 0755 ${app_name}.desktop "$(DESTDIR)$(launcherdir)" 66 | 67 | #app icon 68 | install -m 0755 ./share/pixmaps/${app_name}.svg "$(DESTDIR)$(sharedir)/pixmaps/" 69 | 70 | # appdata 71 | install -m 0755 ../debian/${app_name}.appdata.xml "$(DESTDIR)$(sharedir)/appdata" 72 | 73 | #service 74 | install -m 0755 ${app_name}-service "$(DESTDIR)/etc/init.d/${app_name}" 75 | 76 | #translations 77 | #msgfmt --check --verbose -o "$(DESTDIR)$(localedir)/it_IT/LC_MESSAGES/${app_name}.mo" ../po/${app_name}-it.po 78 | #msgfmt --check --verbose -o "$(DESTDIR)$(localedir)/pt_BR/LC_MESSAGES/${app_name}.mo" ../po/${app_name}-pt.po 79 | #msgfmt --check --verbose -o "$(DESTDIR)$(localedir)/ko_KO/LC_MESSAGES/${app_name}.mo" ../po/${app_name}-ko.po 80 | #msgfmt --check --verbose -o "$(DESTDIR)$(localedir)/ru_RU/LC_MESSAGES/${app_name}.mo" ../po/${app_name}-ru.po 81 | 82 | uninstall: 83 | 84 | # binary 85 | rm -f "$(DESTDIR)$(bindir)/${app_name}" 86 | rm -f "$(DESTDIR)$(bindir)/${app_name}-gtk" 87 | rm -f "$(DESTDIR)$(bindir)/${app_name}-uninstall" 88 | 89 | # shared files 90 | rm -rf "$(DESTDIR)$(sharedir)/${app_name}" 91 | 92 | # launcher 93 | rm -f "$(DESTDIR)$(launcherdir)/${app_name}.desktop" 94 | 95 | # app icon 96 | rm -f "$(DESTDIR)$(sharedir)/pixmaps/${app_name}.svg" 97 | 98 | # app data 99 | rm -f "$(DESTDIR)$(sharedir)/appdata/${app_name}.appdata.xml" 100 | 101 | # service 102 | rm -f "$(DESTDIR)/etc/init.d/${app_name}" 103 | 104 | #translations 105 | rm -f $(DESTDIR)$(localedir)/*/LC_MESSAGES/${app_name}.mo 106 | -------------------------------------------------------------------------------- /src/BatteryBar.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * BatteryBar.vala 3 | * 4 | * Copyright 2017 Tony George 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301, USA. 20 | * 21 | * 22 | */ 23 | 24 | 25 | using Gtk; 26 | using Gee; 27 | 28 | using TeeJee.Logging; 29 | using TeeJee.FileSystem; 30 | using TeeJee.JSON; 31 | using TeeJee.ProcessManagement; 32 | using TeeJee.GtkHelper; 33 | using TeeJee.Multimedia; 34 | using TeeJee.System; 35 | using TeeJee.Misc; 36 | 37 | public class BatteryBar : Window { 38 | 39 | private Gtk.DrawingArea bar; 40 | uint timer_refresh = 0; 41 | 42 | public static int main (string[] args) { 43 | set_locale(); 44 | 45 | Gtk.init(ref args); 46 | 47 | init_tmp(); 48 | 49 | App = new Main(args, true); 50 | 51 | var bar = new BatteryBar(); 52 | bar.destroy.connect(Gtk.main_quit); 53 | bar.show_all(); 54 | 55 | //start event loop 56 | Gtk.main(); 57 | 58 | return 0; 59 | } 60 | 61 | private static void set_locale() { 62 | Intl.setlocale(GLib.LocaleCategory.MESSAGES, AppShortName); 63 | Intl.textdomain(GETTEXT_PACKAGE); 64 | Intl.bind_textdomain_codeset(GETTEXT_PACKAGE, "utf-8"); 65 | Intl.bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR); 66 | } 67 | 68 | public BatteryBar() { 69 | int bar_height = 6; 70 | int screen_width = Gdk.Screen.width(); 71 | int screen_height = Gdk.Screen.height(); 72 | 73 | set_size_request(screen_width, bar_height); 74 | set_decorated(false); 75 | set_keep_above(true); 76 | set_skip_taskbar_hint(true); 77 | set_skip_taskbar_hint(true); 78 | 79 | move(0, screen_height - bar_height); 80 | 81 | this.button_press_event.connect((event) => { 82 | this.close(); 83 | return true; 84 | }); 85 | 86 | bar = new Gtk.DrawingArea(); 87 | bar.expand = true; 88 | add(bar); 89 | 90 | var color_white = Gdk.RGBA(); 91 | color_white.parse("white"); 92 | color_white.alpha = 1.0; 93 | 94 | var color_black = Gdk.RGBA(); 95 | color_black.parse("black"); 96 | color_black.alpha = 1.0; 97 | 98 | var color_red = Gdk.RGBA(); 99 | color_red.parse("red"); 100 | color_red.alpha = 1.0; 101 | 102 | var color_blue_200 = Gdk.RGBA(); 103 | color_blue_200.parse("#90CAF9"); 104 | color_blue_200.alpha = 1.0; 105 | 106 | var color_green_300 = Gdk.RGBA(); 107 | color_green_300.parse("#81C784"); 108 | color_green_300.alpha = 1.0; 109 | 110 | var color_yellow_300 = Gdk.RGBA(); 111 | color_yellow_300.parse("#FFF176"); 112 | color_yellow_300.alpha = 1.0; 113 | 114 | var color_red_300 = Gdk.RGBA(); 115 | color_red_300.parse("#E57373"); 116 | color_red_300.alpha = 1.0; 117 | 118 | Gdk.RGBA color_bar = color_green_300; 119 | 120 | bar.draw.connect ((context) => { 121 | int w = bar.get_allocated_width(); 122 | int h = bar.get_allocated_height(); 123 | 124 | double charge_level = BatteryStat.batt_charge_percent(); 125 | int x_level = (int) ((w * charge_level) / 100.00); 126 | bar.set_tooltip_markup("Battery: %.2f %%".printf(charge_level)); 127 | 128 | if (charge_level >= 60){ 129 | color_bar = color_green_300; 130 | } 131 | else if (charge_level >= 20){ 132 | color_bar = color_yellow_300; 133 | } 134 | else{ 135 | color_bar = color_red_300; 136 | } 137 | 138 | Gdk.cairo_set_source_rgba (context, color_black); 139 | context.set_line_width (1); 140 | 141 | context.rectangle(0, 0, w, h); 142 | context.fill(); 143 | 144 | Gdk.cairo_set_source_rgba (context, color_bar); 145 | context.set_line_width (1); 146 | 147 | context.rectangle(0, 1, x_level, h); 148 | context.fill(); 149 | 150 | return true; 151 | }); 152 | 153 | timer_refresh = Timeout.add(30 * 1000, timer_refresh_bar); 154 | } 155 | 156 | public bool timer_refresh_bar() { 157 | //if (timer_pkg_info > 0){ 158 | // Source.remove(timer_pkg_info); 159 | // timer_pkg_info = 0; 160 | //} 161 | redraw_bar(); 162 | 163 | return true; 164 | } 165 | 166 | private void redraw_bar() { 167 | bar.queue_draw_area(0, 0, bar.get_allocated_width(), 168 | bar.get_allocated_height()); 169 | } 170 | } 171 | 172 | -------------------------------------------------------------------------------- /src/Common/BatteryStat.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * BatteryStat.vala 3 | * 4 | * Copyright 2012-2017 Tony George 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301, USA. 20 | * 21 | * 22 | */ 23 | 24 | using GLib; 25 | using Gtk; 26 | using Gee; 27 | using Json; 28 | 29 | using TeeJee.Logging; 30 | using TeeJee.FileSystem; 31 | using TeeJee.JSON; 32 | using TeeJee.ProcessManagement; 33 | using TeeJee.GtkHelper; 34 | using TeeJee.Multimedia; 35 | using TeeJee.System; 36 | using TeeJee.Misc; 37 | 38 | public class BatteryStat : GLib.Object{ 39 | 40 | public DateTime date; 41 | public long charge_now = 0; 42 | public long voltage_now = 0; 43 | public long cpu_usage = 0; 44 | 45 | public long graph_x = 0; 46 | 47 | public const string BATT_STATS_CHARGE_NOW = "/sys/class/power_supply/BAT0/charge_now"; 48 | public const string BATT_STATS_CHARGE_FULL = "/sys/class/power_supply/BAT0/charge_full"; 49 | public const string BATT_STATS_CHARGE_FULL_DESIGN = "/sys/class/power_supply/BAT0/charge_full_design"; 50 | public const string BATT_STATS_VOLTAGE_NOW = "/sys/class/power_supply/BAT0/voltage_now"; 51 | public const string BATT_STATS_STATUS = "/sys/class/power_supply/BAT0/status"; 52 | 53 | public BatteryStat.read_from_sys(){ 54 | this.date = new DateTime.now_local(); 55 | this.charge_now = batt_charge_now(); 56 | this.voltage_now = batt_voltage_now(); 57 | this.cpu_usage = (long) (ProcStats.get_cpu_usage() * 1000); 58 | } 59 | 60 | public BatteryStat.from_delimited_string(string line){ 61 | var arr = line.split("|"); 62 | if (arr.length == 4){ 63 | DateTime date_utc = new DateTime.from_unix_utc(int64.parse(arr[0])); 64 | this.date = date_utc.to_local(); 65 | this.charge_now = long.parse(arr[1]); 66 | this.voltage_now = long.parse(arr[2]); 67 | this.cpu_usage = long.parse(arr[3]); 68 | } 69 | } 70 | 71 | public string to_delimited_string(){ 72 | var txt = date.to_utc().to_unix().to_string() + "|"; 73 | txt += charge_now.to_string() + "|"; 74 | txt += voltage_now.to_string() + "|"; 75 | txt += cpu_usage.to_string(); 76 | txt += "\n"; 77 | return txt; 78 | } 79 | 80 | public string to_friendly_string(){ 81 | var txt = ""; 82 | txt += date.format("%F %H:%M:%S"); //%d %b %Y, %I:%M %p 83 | txt += ", %6.2f%%, %5.0f mAh, %6.2f Wh".printf( 84 | charge_percent(), 85 | charge_in_mah(), 86 | charge_in_wh() 87 | ); 88 | txt += ", %6.2f V".printf(voltage()); 89 | txt += ", %6.2f %% CPU".printf(cpu_percent()); 90 | txt += "\n"; 91 | return txt; 92 | } 93 | 94 | public double voltage(){ 95 | return (voltage_now / 1000000.00); 96 | } 97 | 98 | public double charge_percent(){ 99 | return (((charge_now * 1.00) / batt_charge_full()) * 100); 100 | } 101 | 102 | public double charge_in_mah(){ 103 | return (charge_now / 1000.0); 104 | } 105 | 106 | public double charge_in_wh(){ 107 | return ((charge_in_mah() * voltage()) / 1000.00); 108 | } 109 | 110 | public double cpu_percent(){ 111 | return (cpu_usage / 1000.00); 112 | } 113 | 114 | public static double batt_charge_percent(){ 115 | return (((batt_charge_now() * 1.00) / batt_charge_full()) * 100); 116 | } 117 | 118 | public static long batt_charge_now(){ 119 | string val = read_sys_stat_file(BATT_STATS_CHARGE_NOW); 120 | if (val.length == 0) { return 0; } 121 | return long.parse(val); 122 | } 123 | 124 | public static long batt_charge_full(){ 125 | string val = read_sys_stat_file(BATT_STATS_CHARGE_FULL); 126 | if (val.length == 0) { return 0; } 127 | return long.parse(val); 128 | } 129 | 130 | public static long batt_charge_full_design(){ 131 | string val = read_sys_stat_file(BATT_STATS_CHARGE_FULL_DESIGN); 132 | if (val.length == 0) { return 0; } 133 | return long.parse(val); 134 | } 135 | 136 | public static double batt_health(){ 137 | if (batt_charge_full_design() > 0){ 138 | return ((batt_charge_full() * 100.0) / batt_charge_full_design()); 139 | } 140 | else{ 141 | return 0; 142 | } 143 | } 144 | 145 | public static long batt_voltage_now(){ 146 | string val = read_sys_stat_file(BATT_STATS_VOLTAGE_NOW); 147 | if (val.length == 0) { return 0; } 148 | return long.parse(val); 149 | } 150 | 151 | public static bool is_charging(){ 152 | string val = read_sys_stat_file(BATT_STATS_STATUS); 153 | return (val == "Charging"); 154 | } 155 | 156 | public static string read_sys_stat_file(string statFile){ 157 | try{ 158 | var file = File.new_for_path(statFile); 159 | if (file.query_exists()){ 160 | var dis = new DataInputStream (file.read()); 161 | string line = dis.read_line (null); 162 | if (line != null) { 163 | return line; 164 | } 165 | } //stream closed 166 | } 167 | catch (Error e){ 168 | log_error (e.message); 169 | } 170 | 171 | return ""; 172 | } 173 | } 174 | 175 | -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/config.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 11 | 13 | 15 | 19 | 23 | 24 | 34 | 36 | 40 | 44 | 45 | 53 | 60 | 64 | 68 | 69 | 77 | 79 | 83 | 87 | 88 | 97 | 98 | 100 | 104 | 108 | 112 | 116 | 117 | 118 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | GNU LESSER GENERAL PUBLIC LICENSE 3 | Version 3, 29 June 2007 4 | 5 | Copyright (C) 2007 Free Software Foundation, Inc. 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | 10 | This version of the GNU Lesser General Public License incorporates 11 | the terms and conditions of version 3 of the GNU General Public 12 | License, supplemented by the additional permissions listed below. 13 | 14 | 0. Additional Definitions. 15 | 16 | As used herein, "this License" refers to version 3 of the GNU Lesser 17 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 18 | General Public License. 19 | 20 | "The Library" refers to a covered work governed by this License, 21 | other than an Application or a Combined Work as defined below. 22 | 23 | An "Application" is any work that makes use of an interface provided 24 | by the Library, but which is not otherwise based on the Library. 25 | Defining a subclass of a class defined by the Library is deemed a mode 26 | of using an interface provided by the Library. 27 | 28 | A "Combined Work" is a work produced by combining or linking an 29 | Application with the Library. The particular version of the Library 30 | with which the Combined Work was made is also called the "Linked 31 | Version". 32 | 33 | The "Minimal Corresponding Source" for a Combined Work means the 34 | Corresponding Source for the Combined Work, excluding any source code 35 | for portions of the Combined Work that, considered in isolation, are 36 | based on the Application, and not on the Linked Version. 37 | 38 | The "Corresponding Application Code" for a Combined Work means the 39 | object code and/or source code for the Application, including any data 40 | and utility programs needed for reproducing the Combined Work from the 41 | Application, but excluding the System Libraries of the Combined Work. 42 | 43 | 1. Exception to Section 3 of the GNU GPL. 44 | 45 | You may convey a covered work under sections 3 and 4 of this License 46 | without being bound by section 3 of the GNU GPL. 47 | 48 | 2. Conveying Modified Versions. 49 | 50 | If you modify a copy of the Library, and, in your modifications, a 51 | facility refers to a function or data to be supplied by an Application 52 | that uses the facility (other than as an argument passed when the 53 | facility is invoked), then you may convey a copy of the modified 54 | version: 55 | 56 | a) under this License, provided that you make a good faith effort to 57 | ensure that, in the event an Application does not supply the 58 | function or data, the facility still operates, and performs 59 | whatever part of its purpose remains meaningful, or 60 | 61 | b) under the GNU GPL, with none of the additional permissions of 62 | this License applicable to that copy. 63 | 64 | 3. Object Code Incorporating Material from Library Header Files. 65 | 66 | The object code form of an Application may incorporate material from 67 | a header file that is part of the Library. You may convey such object 68 | code under terms of your choice, provided that, if the incorporated 69 | material is not limited to numerical parameters, data structure 70 | layouts and accessors, or small macros, inline functions and templates 71 | (ten or fewer lines in length), you do both of the following: 72 | 73 | a) Give prominent notice with each copy of the object code that the 74 | Library is used in it and that the Library and its use are 75 | covered by this License. 76 | 77 | b) Accompany the object code with a copy of the GNU GPL and this license 78 | document. 79 | 80 | 4. Combined Works. 81 | 82 | You may convey a Combined Work under terms of your choice that, 83 | taken together, effectively do not restrict modification of the 84 | portions of the Library contained in the Combined Work and reverse 85 | engineering for debugging such modifications, if you also do each of 86 | the following: 87 | 88 | a) Give prominent notice with each copy of the Combined Work that 89 | the Library is used in it and that the Library and its use are 90 | covered by this License. 91 | 92 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 93 | document. 94 | 95 | c) For a Combined Work that displays copyright notices during 96 | execution, include the copyright notice for the Library among 97 | these notices, as well as a reference directing the user to the 98 | copies of the GNU GPL and this license document. 99 | 100 | d) Do one of the following: 101 | 102 | 0) Convey the Minimal Corresponding Source under the terms of this 103 | License, and the Corresponding Application Code in a form 104 | suitable for, and under terms that permit, the user to 105 | recombine or relink the Application with a modified version of 106 | the Linked Version to produce a modified Combined Work, in the 107 | manner specified by section 6 of the GNU GPL for conveying 108 | Corresponding Source. 109 | 110 | 1) Use a suitable shared library mechanism for linking with the 111 | Library. A suitable mechanism is one that (a) uses at run time 112 | a copy of the Library already present on the user's computer 113 | system, and (b) will operate properly with a modified version 114 | of the Library that is interface-compatible with the Linked 115 | Version. 116 | 117 | e) Provide Installation Information, but only if you would otherwise 118 | be required to provide such information under section 6 of the 119 | GNU GPL, and only to the extent that such information is 120 | necessary to install and execute a modified version of the 121 | Combined Work produced by recombining or relinking the 122 | Application with a modified version of the Linked Version. (If 123 | you use option 4d0, the Installation Information must accompany 124 | the Minimal Corresponding Source and Corresponding Application 125 | Code. If you use option 4d1, you must provide the Installation 126 | Information in the manner specified by section 6 of the GNU GPL 127 | for conveying Corresponding Source.) 128 | 129 | 5. Combined Libraries. 130 | 131 | You may place library facilities that are a work based on the 132 | Library side by side in a single library together with other library 133 | facilities that are not Applications and are not covered by this 134 | License, and convey such a combined library under terms of your 135 | choice, if you do both of the following: 136 | 137 | a) Accompany the combined library with a copy of the same work based 138 | on the Library, uncombined with any other library facilities, 139 | conveyed under the terms of this License. 140 | 141 | b) Give prominent notice with the combined library that part of it 142 | is a work based on the Library, and explaining where to find the 143 | accompanying uncombined form of the same work. 144 | 145 | 6. Revised Versions of the GNU Lesser General Public License. 146 | 147 | The Free Software Foundation may publish revised and/or new versions 148 | of the GNU Lesser General Public License from time to time. Such new 149 | versions will be similar in spirit to the present version, but may 150 | differ in detail to address new problems or concerns. 151 | 152 | Each version is given a distinguishing version number. If the 153 | Library as you received it specifies that a certain numbered version 154 | of the GNU Lesser General Public License "or any later version" 155 | applies to it, you have the option of following the terms and 156 | conditions either of that published version or of any later version 157 | published by the Free Software Foundation. If the Library as you 158 | received it does not specify a version number of the GNU Lesser 159 | General Public License, you may choose any version of the GNU Lesser 160 | General Public License ever published by the Free Software Foundation. 161 | 162 | If the Library as you received it specifies that a proxy can decide 163 | whether future versions of the GNU Lesser General Public License shall 164 | apply, that proxy's public statement of acceptance of any version is 165 | permanent authorization for you to choose that version for the 166 | Library. 167 | -------------------------------------------------------------------------------- /src/Gtk/AboutWindow.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * AboutWindow.vala 3 | * 4 | * Copyright 2012-2017 Tony George 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301, USA. 20 | * 21 | * 22 | */ 23 | 24 | using Gtk; 25 | 26 | using TeeJee.Logging; 27 | using TeeJee.FileSystem; 28 | using TeeJee.JSON; 29 | using TeeJee.ProcessManagement; 30 | using TeeJee.GtkHelper; 31 | using TeeJee.System; 32 | using TeeJee.Misc; 33 | 34 | public class AboutWindow : Dialog { 35 | private Box vbox_main; 36 | private Box vbox_logo; 37 | private Box vbox_credits; 38 | private Box vbox_lines; 39 | private Box hbox_action; 40 | private Button btn_credits; 41 | private Button btn_close; 42 | 43 | private Gtk.Image img_logo; 44 | private Label lbl_program_name; 45 | private Label lbl_version; 46 | private Label lbl_comments; 47 | private LinkButton lbtn_website; 48 | private Label lbl_copyright; 49 | 50 | private string[] _artists; 51 | public string[] artists{ 52 | get{ 53 | return _artists; 54 | } 55 | set{ 56 | _artists = value; 57 | } 58 | } 59 | 60 | private string[] _authors; 61 | public string[] authors{ 62 | get{ 63 | return _authors; 64 | } 65 | set{ 66 | _authors = value; 67 | } 68 | } 69 | 70 | private string _comments = ""; 71 | public string comments{ 72 | get{ 73 | return _comments; 74 | } 75 | set{ 76 | _comments = value; 77 | } 78 | } 79 | 80 | private string _copyright = ""; 81 | public string copyright{ 82 | get{ 83 | return _copyright; 84 | } 85 | set{ 86 | _copyright = value; 87 | } 88 | } 89 | 90 | private string[] _documenters; 91 | public string[] documenters{ 92 | get{ 93 | return _documenters; 94 | } 95 | set{ 96 | _documenters = value; 97 | } 98 | } 99 | 100 | private string[] _donations; 101 | public string[] donations{ 102 | get{ 103 | return _donations; 104 | } 105 | set{ 106 | _donations = value; 107 | } 108 | } 109 | 110 | private string _license = ""; 111 | public string license{ 112 | get{ 113 | return _license; 114 | } 115 | set{ 116 | _license = value; 117 | } 118 | } 119 | 120 | private Gdk.Pixbuf _logo; 121 | public Gdk.Pixbuf logo{ 122 | get{ 123 | return _logo; 124 | } 125 | set{ 126 | _logo = value; 127 | } 128 | } 129 | 130 | private string _program_name = ""; 131 | public string program_name{ 132 | get{ 133 | return _program_name; 134 | } 135 | set{ 136 | _program_name = value; 137 | } 138 | } 139 | 140 | private string[] _translators; 141 | public string[] translators{ 142 | get{ 143 | return _translators; 144 | } 145 | set{ 146 | _translators = value; 147 | } 148 | } 149 | 150 | private string[] _third_party; 151 | public string[] third_party{ 152 | get{ 153 | return _third_party; 154 | } 155 | set{ 156 | _third_party = value; 157 | } 158 | } 159 | 160 | private string _version = ""; 161 | public string version{ 162 | get{ 163 | return _version; 164 | } 165 | set{ 166 | _version = value; 167 | } 168 | } 169 | 170 | private string _website = ""; 171 | public string website{ 172 | get{ 173 | return _website; 174 | } 175 | set{ 176 | _website = value; 177 | } 178 | } 179 | 180 | private string _website_label = ""; 181 | public string website_label{ 182 | get{ 183 | return _website_label; 184 | } 185 | set{ 186 | _website_label = value; 187 | } 188 | } 189 | 190 | public AboutWindow() { 191 | window_position = WindowPosition.CENTER_ON_PARENT; 192 | set_destroy_with_parent (true); 193 | set_modal (true); 194 | skip_taskbar_hint = false; 195 | set_default_size (450, 400); 196 | 197 | vbox_main = get_content_area(); 198 | vbox_main.margin = 6; 199 | vbox_main.spacing = 6; 200 | 201 | vbox_logo = new Box(Orientation.VERTICAL,0); 202 | vbox_main.add(vbox_logo); 203 | 204 | vbox_credits = new Box(Orientation.VERTICAL,0); 205 | vbox_credits.no_show_all = true; 206 | vbox_main.add(vbox_credits); 207 | 208 | vbox_lines = new Box(Orientation.VERTICAL,0); 209 | vbox_lines.margin_top = 10; 210 | 211 | //logo 212 | img_logo = new Gtk.Image(); 213 | img_logo.margin_top = 6; 214 | img_logo.margin_bottom = 6; 215 | vbox_logo.add(img_logo); 216 | 217 | //program_name 218 | lbl_program_name = new Label(""); 219 | lbl_program_name.set_use_markup(true); 220 | vbox_logo.add(lbl_program_name); 221 | 222 | //version 223 | lbl_version = new Label(""); 224 | lbl_version.set_use_markup(true); 225 | lbl_version.margin_top = 5; 226 | vbox_logo.add(lbl_version); 227 | 228 | //comments 229 | lbl_comments = new Label(""); 230 | lbl_comments.set_use_markup(true); 231 | lbl_comments.margin_top = 10; 232 | vbox_logo.add(lbl_comments); 233 | 234 | //website 235 | lbtn_website = new LinkButton(""); 236 | lbtn_website.margin_top = 5; 237 | vbox_logo.add(lbtn_website); 238 | 239 | lbtn_website.activate_link.connect(()=>{ 240 | try{ 241 | return Gtk.show_uri(null, lbtn_website.uri, Gdk.CURRENT_TIME); 242 | } 243 | catch(Error e){ 244 | return false; 245 | } 246 | }); 247 | 248 | //copyright 249 | lbl_copyright = new Label(""); 250 | lbl_copyright.set_use_markup(true); 251 | lbl_copyright.margin_top = 5; 252 | vbox_logo.add(lbl_copyright); 253 | 254 | //spacer_bottom 255 | var spacer_bottom = new Label(""); 256 | spacer_bottom.margin_top = 20; 257 | vbox_logo.add(spacer_bottom); 258 | 259 | //scroller 260 | var sw_credits = new ScrolledWindow(null, null); 261 | sw_credits.set_shadow_type(ShadowType.ETCHED_IN); 262 | sw_credits.expand = true; 263 | 264 | vbox_credits.add(sw_credits); 265 | sw_credits.add(vbox_lines); 266 | 267 | //hbox_commands -------------------------------------------------- 268 | 269 | hbox_action = (Box) get_action_area(); 270 | 271 | //btn_credits 272 | btn_credits = new Button.with_label(" " + _("Credits")); 273 | btn_credits.set_image (new Image.from_stock ("gtk-about", IconSize.MENU)); 274 | hbox_action.add(btn_credits); 275 | 276 | btn_credits.clicked.connect(()=>{ 277 | vbox_logo.visible = !(vbox_logo.visible); 278 | vbox_credits.visible = !(vbox_credits.visible); 279 | 280 | if ((vbox_credits.visible)&&(!sw_credits.visible)){ 281 | sw_credits.show_all(); 282 | } 283 | 284 | if (vbox_credits.visible){ 285 | btn_credits.label = " " + _("Back"); 286 | btn_credits.set_image (new Image.from_stock ("gtk-go-back", IconSize.MENU)); 287 | } 288 | else{ 289 | btn_credits.label = " " + _("Credits"); 290 | btn_credits.set_image (new Image.from_stock ("gtk-about", IconSize.MENU)); 291 | } 292 | }); 293 | 294 | //btn_close 295 | btn_close = new Button.with_label(" " + _("Close")); 296 | btn_close.set_image (new Image.from_stock ("gtk-close", IconSize.MENU)); 297 | hbox_action.add(btn_close); 298 | 299 | btn_close.clicked.connect(()=>{ this.destroy(); }); 300 | } 301 | 302 | public void initialize() { 303 | title = program_name; 304 | img_logo.pixbuf = logo; 305 | lbl_program_name.label = "%s".printf(program_name); 306 | lbl_version.label = "v%s".printf(version); 307 | lbl_comments.label = "%s".printf(comments); 308 | lbtn_website.uri = website; 309 | lbtn_website.label = website_label; 310 | //lbl_copyright.label = "%s".printf(copyright); 311 | lbl_copyright.label = "%s".printf(copyright); 312 | 313 | if (authors.length > 0){ 314 | add_line("%s\n".printf(_("Authors"))); 315 | foreach(string name in authors){ 316 | add_line("%s\n".printf(name)); 317 | } 318 | add_line("\n"); 319 | } 320 | 321 | if (third_party.length > 0){ 322 | add_line("%s\n".printf(_("Third Party Tools"))); 323 | foreach(string name in third_party){ 324 | add_line("%s\n".printf(name)); 325 | } 326 | add_line("\n"); 327 | } 328 | 329 | if (artists.length > 0){ 330 | add_line("%s\n".printf(_("Artists"))); 331 | foreach(string name in artists){ 332 | add_line("%s\n".printf(name)); 333 | } 334 | add_line("\n"); 335 | } 336 | 337 | if (translators.length > 0){ 338 | add_line("%s\n".printf(_("Translators"))); 339 | foreach(string name in translators){ 340 | add_line("%s\n".printf(name)); 341 | } 342 | add_line("\n"); 343 | } 344 | 345 | if (documenters.length > 0){ 346 | add_line("%s\n".printf(_("Documenters"))); 347 | foreach(string name in documenters){ 348 | add_line("%s\n".printf(name)); 349 | } 350 | add_line("\n"); 351 | } 352 | 353 | if (donations.length > 0){ 354 | add_line("%s\n".printf(_("Donations"))); 355 | foreach(string name in donations){ 356 | add_line("%s\n".printf(name)); 357 | } 358 | add_line("\n"); 359 | } 360 | 361 | if (vbox_lines.get_children().length() == 0){ 362 | btn_credits.visible = false; 363 | } 364 | } 365 | 366 | public void add_line(string text){ 367 | if (text.split(":").length >= 2){ 368 | var link = new LinkButton(text.split(":")[0]); 369 | vbox_lines.add(link); 370 | 371 | string val = text[text.index_of(":") + 1:text.length]; 372 | if (val.contains("@")){ 373 | link.uri = "mailto:" + val; 374 | } 375 | else if(val.has_prefix("http://")){ 376 | link.uri = val; 377 | } 378 | else{ 379 | link.uri = "http://" + val; 380 | } 381 | 382 | link.activate_link.connect(()=>{ 383 | try{ 384 | return Gtk.show_uri(null, link.uri, Gdk.CURRENT_TIME); 385 | } 386 | catch(Error e){ 387 | return false; 388 | } 389 | }); 390 | } 391 | else{ 392 | var lbl = new Label(text); 393 | lbl.set_use_markup(true); 394 | lbl.valign = Align.START; 395 | lbl.wrap = true; 396 | lbl.wrap_mode = Pango.WrapMode.WORD; 397 | vbox_lines.add(lbl); 398 | } 399 | } 400 | } 401 | -------------------------------------------------------------------------------- /src/Common/Main.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * Main.vala 3 | * 4 | * Copyright 2012-2017 Tony George 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301, USA. 20 | * 21 | * 22 | */ 23 | 24 | using GLib; 25 | using Gtk; 26 | using Gee; 27 | using Json; 28 | 29 | using TeeJee.Logging; 30 | using TeeJee.FileSystem; 31 | using TeeJee.JSON; 32 | using TeeJee.ProcessManagement; 33 | using TeeJee.GtkHelper; 34 | using TeeJee.Multimedia; 35 | using TeeJee.System; 36 | using TeeJee.Misc; 37 | 38 | public Main App; 39 | public const string AppName = "Aptik Battery Monitor"; 40 | public const string AppShortName = "aptik-battery-monitor"; 41 | public const string AppVersion = "17.12"; 42 | public const string AppAuthor = "Tony George"; 43 | public const string AppAuthorEmail = "teejeetech@gmail.com"; 44 | 45 | const string GETTEXT_PACKAGE = ""; 46 | const string LOCALE_DIR = "/usr/share/locale"; 47 | 48 | extern void exit(int exit_code); 49 | 50 | public class Main : GLib.Object { 51 | public static string BATT_STATS_CACHE_FILE = "/var/log/aptik-battery-monitor/stats.log"; 52 | public static string BATT_STATS_HIST_FILE = "/var/log/aptik-battery-monitor/history.log"; 53 | 54 | public static int BATT_STATS_LOG_INTERVAL = 30; 55 | public static double BATT_STATS_ARCHIVE_LEVEL = 99.00; 56 | 57 | public bool gui_mode = false; 58 | public string user_login = ""; 59 | public string user_home = ""; 60 | public int user_uid = -1; 61 | 62 | public bool print_stats = false; 63 | public string command = ""; 64 | 65 | public string temp_dir = ""; 66 | public string backup_dir = ""; 67 | public string share_dir = "/usr/share"; 68 | public string app_conf_path = ""; 69 | 70 | public Gee.ArrayList battery_stats_list; 71 | public BatteryStat stat_current; 72 | public BatteryStat stat_prev; 73 | public BatteryStat stat_prev2; 74 | 75 | public Main(string[] args, bool _gui_mode) { 76 | 77 | gui_mode = _gui_mode; 78 | 79 | init_tmp(); 80 | 81 | //config file 82 | string home = Environment.get_home_dir(); 83 | app_conf_path = home + "/.config/aptik-battery-monitor.json"; 84 | 85 | //load settings if GUI mode 86 | if (gui_mode) { 87 | load_app_config(); 88 | } 89 | 90 | //check dependencies 91 | string message; 92 | if (!check_dependencies(out message)) { 93 | if (gui_mode) { 94 | string title = _("Missing Dependencies"); 95 | gtk_messagebox(title, message, null, true); 96 | } 97 | exit(0); 98 | } 99 | 100 | //initialize backup_dir as current directory for CLI mode 101 | if (!gui_mode) { 102 | backup_dir = Environment.get_current_dir() + "/"; 103 | } 104 | 105 | try { 106 | //create temp dir 107 | temp_dir = get_temp_file_path(); 108 | 109 | var f = File.new_for_path(temp_dir); 110 | if (f.query_exists()) { 111 | Posix.system("rm -rf %s".printf(temp_dir)); 112 | } 113 | f.make_directory_with_parents(); 114 | } 115 | catch (Error e) { 116 | log_error (e.message); 117 | } 118 | 119 | //get user info 120 | user_login = get_user_login(); 121 | user_home = "/home/" + user_login; 122 | user_uid = get_user_id(user_login); 123 | 124 | //BATT_STATS_CACHE_FILE = "%s/.local/log/aptik-battery-monitor/stats.log".printf(user_home); 125 | 126 | battery_stats_list = new Gee.ArrayList(); 127 | } 128 | 129 | public void check_for_multiple_instances(){ 130 | var app_lock = new ApplicationLock("aptik-battery-monitor", false); 131 | if (app_lock.another_instance_is_running()){ 132 | exit(0); 133 | } 134 | else{ 135 | app_lock.create_lock(); 136 | } 137 | } 138 | 139 | public bool check_dependencies(out string msg) { 140 | msg = ""; 141 | 142 | string[] dependencies = { "grep", "find" }; 143 | 144 | string path; 145 | foreach(string cmd_tool in dependencies) { 146 | path = get_cmd_path (cmd_tool); 147 | if ((path == null) || (path.length == 0)) { 148 | msg += " * " + cmd_tool + "\n"; 149 | } 150 | } 151 | 152 | if (msg.length > 0) { 153 | msg = _("Commands listed below are not available on this system") + ":\n\n" + msg + "\n"; 154 | msg += _("Please install the required packages"); 155 | log_msg(msg); 156 | return false; 157 | } 158 | else { 159 | return true; 160 | } 161 | } 162 | 163 | /* Common */ 164 | 165 | public string create_log_dir() { 166 | string log_dir = backup_dir + "logs/" + timestamp3(); 167 | create_dir(log_dir); 168 | return log_dir; 169 | } 170 | 171 | public void save_app_config() { 172 | var config = new Json.Object(); 173 | 174 | var json = new Json.Generator(); 175 | json.pretty = true; 176 | json.indent = 2; 177 | var node = new Json.Node(NodeType.OBJECT); 178 | node.set_object(config); 179 | json.set_root(node); 180 | 181 | try { 182 | json.to_file(this.app_conf_path); 183 | } catch (Error e) { 184 | log_error (e.message); 185 | } 186 | 187 | if (gui_mode) { 188 | log_msg(_("App config saved") + ": '%s'".printf(app_conf_path)); 189 | } 190 | } 191 | 192 | public void load_app_config() { 193 | var f = File.new_for_path(app_conf_path); 194 | if (!f.query_exists()) { 195 | return; 196 | } 197 | 198 | var parser = new Json.Parser(); 199 | try { 200 | parser.load_from_file(this.app_conf_path); 201 | } 202 | catch (Error e) { 203 | log_error (e.message); 204 | } 205 | 206 | //var node = parser.get_root(); 207 | //var config = node.get_object(); 208 | 209 | if (gui_mode) { 210 | log_msg(_("App config loaded") + ": '%s'".printf(this.app_conf_path)); 211 | } 212 | } 213 | 214 | public void exit_app() { 215 | 216 | save_app_config(); 217 | 218 | try { 219 | //delete temporary files 220 | var f = File.new_for_path(temp_dir); 221 | if (f.query_exists()) { 222 | f.delete(); 223 | } 224 | } 225 | catch (Error e) { 226 | log_error (e.message); 227 | } 228 | } 229 | 230 | /* Battery Stats */ 231 | 232 | public void log_battery_stats(bool print_stats) { 233 | try { 234 | // get stats ---------------------- 235 | 236 | var stat = new BatteryStat.read_from_sys(); 237 | stat_current = stat; 238 | 239 | //create or open log 240 | var file = File.new_for_path(BATT_STATS_CACHE_FILE); 241 | if (!file.query_exists()) { 242 | create_empty_log_file(BATT_STATS_CACHE_FILE); 243 | battery_stats_list.clear(); 244 | } 245 | 246 | //check if log needs rotation 247 | check_and_rotate_log(); 248 | 249 | //add entry to log 250 | battery_stats_list.add(stat); 251 | var fos = file.append_to (FileCreateFlags.NONE); 252 | var dos = new DataOutputStream (fos); 253 | dos.put_string(stat.to_delimited_string()); 254 | if (print_stats) { 255 | stdout.printf(stat.to_friendly_string()); 256 | } 257 | 258 | //rotate references 259 | stat_prev2 = stat_prev; 260 | stat_prev = stat_current; 261 | } 262 | catch (Error e) { 263 | log_error (e.message); 264 | } 265 | } 266 | 267 | public bool check_and_rotate_log(){ 268 | // rotates log file when battery drops after removing from charger 269 | 270 | bool rotated = false; 271 | 272 | if ((stat_prev != null) && (stat_prev2 != null)) { 273 | 274 | //check if charge levels for [stat_prev2, stat_prev, stat] are like 275 | //[ 79.80, 80.00, 79.50] - increase and then decrease, or like 276 | //[100.00, 100.00, 99.70] - same values and then decrease 277 | 278 | if((stat_current.charge_percent() < stat_prev.charge_percent()) 279 | && ( (stat_prev.charge_percent() > stat_prev2.charge_percent()) 280 | || (stat_prev2.charge_percent() == stat_prev.charge_percent()) 281 | )) 282 | { 283 | 284 | if (print_stats) { 285 | stdout.printf("\n[" + _("Removed from charger") + "]\n\n"); 286 | } 287 | 288 | log_battery_cycle_summary(); 289 | 290 | // create or open log ------------ 291 | var file = File.new_for_path(BATT_STATS_CACHE_FILE); 292 | if (!file.query_exists()) { 293 | create_empty_log_file(BATT_STATS_CACHE_FILE); 294 | battery_stats_list.clear(); 295 | } 296 | var date_label = (new DateTime.now_local()).format("%F_%H-%M-%S"); 297 | var archive = File.new_for_path(BATT_STATS_CACHE_FILE + "." + date_label); 298 | 299 | try{ 300 | file.move(archive, FileCopyFlags.NONE); 301 | create_empty_log_file(BATT_STATS_CACHE_FILE); 302 | rotated = true; 303 | } 304 | catch(Error e){ 305 | log_error (e.message); 306 | } 307 | 308 | if (print_stats) { 309 | stdout.printf(_("Archived") + ": '%s'\n".printf(archive.get_path())); 310 | stdout.printf(_("Logging stats to file") + ": '%s'\n\n".printf(BATT_STATS_CACHE_FILE)); 311 | } 312 | } 313 | 314 | if ((stat_current.charge_percent() > stat_prev.charge_percent()) 315 | && (stat_prev.charge_percent() < stat_prev2.charge_percent())) 316 | { 317 | if (print_stats) { 318 | stdout.printf("\n[" + _("Charging") + "]\n\n"); 319 | } 320 | } 321 | } 322 | 323 | return rotated; 324 | } 325 | 326 | public void log_battery_cycle_summary(){ 327 | try { 328 | //create or open hist log 329 | var file = File.new_for_path(BATT_STATS_HIST_FILE); 330 | if (!file.query_exists()) { 331 | create_empty_log_file(BATT_STATS_HIST_FILE); 332 | } 333 | 334 | var cycle = new BatteryCycle(); 335 | cycle.calculate_stats(App.battery_stats_list); 336 | 337 | //log cycle summary 338 | var fos = file.append_to (FileCreateFlags.NONE); 339 | var dos = new DataOutputStream (fos); 340 | dos.put_string(cycle.to_delimited_string()); 341 | if (print_stats) { 342 | stdout.printf(_("Logging summary to file") + ": '%s'\n".printf(BATT_STATS_HIST_FILE)); 343 | stdout.printf(cycle.to_friendly_string() + "\n"); 344 | } 345 | } 346 | catch (Error e) { 347 | log_error (e.message); 348 | } 349 | } 350 | 351 | private void create_empty_log_file(string file_path) { 352 | try { 353 | var file = File.new_for_path(file_path); 354 | var parent_dir = file.get_parent(); 355 | 356 | if (!parent_dir.query_exists()) { 357 | parent_dir.make_directory_with_parents(); 358 | Posix.system("chmod a+rwx '%s'".printf(parent_dir.get_path())); 359 | } 360 | 361 | if (!file.query_exists()) { 362 | Posix.system("touch '%s'".printf(file.get_path())); 363 | Posix.system("chmod a+rwx '%s'".printf(file.get_path())); 364 | } 365 | } 366 | catch (Error e) { 367 | log_error (e.message); 368 | } 369 | } 370 | 371 | public void read_battery_stats() { 372 | log_debug("call: read_battery_stats"); 373 | var timer = timer_start(); 374 | 375 | try { 376 | battery_stats_list = new Gee.ArrayList(); 377 | 378 | var file = File.new_for_path (BATT_STATS_CACHE_FILE); 379 | if (file.query_exists ()) { 380 | var dis = new DataInputStream (file.read()); 381 | 382 | string line; 383 | while ((line = dis.read_line (null)) != null) { 384 | var stat = new BatteryStat.from_delimited_string(line); 385 | battery_stats_list.add(stat); 386 | } 387 | 388 | if (battery_stats_list.size >= 1){ 389 | stat_current = battery_stats_list[battery_stats_list.size - 1]; 390 | } 391 | if (battery_stats_list.size >= 2){ 392 | stat_prev = battery_stats_list[battery_stats_list.size - 2]; 393 | } 394 | if (battery_stats_list.size >= 3){ 395 | stat_prev2 = battery_stats_list[battery_stats_list.size - 3]; 396 | } 397 | 398 | log_debug("read_battery_stats: %s".printf(timer_elapsed_string(timer))); 399 | } 400 | else { 401 | log_error (_("File not found") + ": %s".printf(BATT_STATS_CACHE_FILE)); 402 | } 403 | } 404 | catch (Error e) { 405 | log_error (e.message); 406 | } 407 | } 408 | 409 | public void print_log_file(){ 410 | BatteryStat stat_last = null; 411 | foreach (BatteryStat stat in App.battery_stats_list){ 412 | if ((stat_last != null) 413 | && (stat_last.date.add_seconds(Main.BATT_STATS_LOG_INTERVAL + 1).compare(stat.date) < 0)) 414 | { 415 | stdout.printf(string.nfill(79, '-') + "\n"); 416 | 417 | var diff = stat.date.difference(stat_last.date); 418 | 419 | var hr = (int64) (diff / (1000000.0 * 60 * 60)); 420 | diff = (int64) (diff % (1000000.0 * 60 * 60)); 421 | var min = (int64) (diff / (1000000.0 * 60)); 422 | diff = (int64) (diff % (1000000.0 * 60)); 423 | var sec = (int64) (diff / (1000000.0)); 424 | 425 | stdout.printf("Gap: %02lld:%02lld:%02lld\n".printf(hr,min,sec)); 426 | stdout.printf(string.nfill(79, '-') + "\n"); 427 | } 428 | 429 | stdout.printf(stat.to_friendly_string()); 430 | stat_last = stat; 431 | } 432 | } 433 | } 434 | 435 | -------------------------------------------------------------------------------- /src/share/pixmaps/aptik-battery-monitor.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 11 | 13 | 15 | 19 | 23 | 24 | 34 | 36 | 40 | 44 | 45 | 55 | 57 | 61 | 65 | 66 | 76 | 78 | 82 | 86 | 87 | 96 | 98 | 102 | 106 | 107 | 116 | 118 | 122 | 126 | 127 | 136 | 138 | 142 | 146 | 147 | 156 | 158 | 162 | 166 | 167 | 176 | 178 | 182 | 186 | 187 | 197 | 199 | 203 | 207 | 208 | 217 | 219 | 223 | 227 | 228 | 238 | 240 | 244 | 248 | 249 | 259 | 260 | 262 | 266 | 270 | 274 | 278 | 282 | 286 | 290 | 292 | 296 | 300 | 304 | 305 | 306 | 307 | -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/help-info.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 21 | 31 | 34 | 38 | 42 | 43 | 53 | 56 | 60 | 64 | 65 | 75 | 78 | 82 | 86 | 87 | 97 | 106 | 108 | 112 | 116 | 117 | 127 | 129 | 133 | 137 | 141 | 145 | 146 | 157 | 159 | 163 | 167 | 171 | 175 | 176 | 186 | 188 | 192 | 196 | 197 | 208 | 209 | 228 | 230 | 231 | 233 | image/svg+xml 234 | 236 | 237 | 238 | 239 | 240 | 244 | 249 | 254 | 259 | 264 | 269 | 274 | 279 | 284 | 290 | 291 | 292 | -------------------------------------------------------------------------------- /src/share/aptik-battery-monitor/images/donate.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 12 | 14 | 16 | 20 | 24 | 25 | 34 | 43 | 45 | 49 | 53 | 54 | 64 | 65 | 69 | 72 | 76 | 80 | 84 | 88 | 92 | 96 | 100 | 104 | 108 | 112 | 116 | 117 | 120 | 124 | 128 | 132 | 136 | 140 | 144 | 148 | 152 | 156 | 160 | 164 | 165 | 168 | 172 | 176 | 183 | 190 | 194 | 201 | 205 | 209 | 213 | 217 | 221 | 225 | 226 | 229 | 233 | 237 | 241 | 245 | 249 | 253 | 257 | 258 | 261 | 265 | 269 | 276 | 283 | 287 | 291 | 295 | 299 | 303 | 310 | 317 | 321 | 325 | 326 | 329 | 333 | 337 | 344 | 351 | 355 | 362 | 366 | 370 | 374 | 378 | 382 | 386 | 387 | 390 | 394 | 398 | 405 | 412 | 416 | 420 | 424 | 428 | 432 | 439 | 446 | 450 | 454 | 455 | 458 | 459 | -------------------------------------------------------------------------------- /src/Gtk/BatteryStatsWindow.vala: -------------------------------------------------------------------------------- 1 | /* 2 | * BatteryStatsWindow.vala 3 | * 4 | * Copyright 2012-2017 Tony George 5 | * 6 | * This program is free software; you can redistribute it and/or modify 7 | * it under the terms of the GNU General Public License as published by 8 | * the Free Software Foundation; either version 2 of the License, or 9 | * (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 | * GNU General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU General Public License 17 | * along with this program; if not, write to the Free Software 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 | * MA 02110-1301, USA. 20 | * 21 | * 22 | */ 23 | 24 | 25 | using Gtk; 26 | using Gee; 27 | 28 | using TeeJee.Logging; 29 | using TeeJee.FileSystem; 30 | using TeeJee.JSON; 31 | using TeeJee.ProcessManagement; 32 | using TeeJee.GtkHelper; 33 | using TeeJee.Multimedia; 34 | using TeeJee.System; 35 | using TeeJee.Misc; 36 | 37 | public class BatteryStatsWindow : Window { 38 | 39 | private Box vbox_main; 40 | 41 | private Gtk.DrawingArea drawing_area; 42 | 43 | private Label lbl_stats_line_batt; 44 | private Label lbl_stats_line_used; 45 | 46 | private Label lbl_percent; 47 | private Label lbl_percent_val; 48 | private Label lbl_remaining; 49 | private Label lbl_remaining_val; 50 | private Label lbl_avg_life; 51 | private Label lbl_avg_life_val; 52 | private Label lbl_time_to_charge; 53 | private Label lbl_time_to_charge_val; 54 | private Label lbl_rate; 55 | private Label lbl_rate_val; 56 | private Label lbl_health; 57 | private Label lbl_health_val; 58 | 59 | private Gtk.Image img_battery_status; 60 | 61 | BatteryStat stat_current; 62 | int X_INTERVAL = 5; 63 | int X_OFFSET = 30; 64 | int Y_OFFSET = 20; 65 | 66 | uint timer_refresh = 0; 67 | 68 | int def_width = 500; 69 | int def_height = 500; 70 | 71 | private Gdk.RGBA color_white; 72 | private Gdk.RGBA color_black; 73 | private Gdk.RGBA color_red; 74 | private Gdk.RGBA color_blue; 75 | private Gdk.RGBA color_red_100; 76 | 77 | public BatteryStatsWindow() { 78 | destroy.connect(Gtk.main_quit); 79 | init_window(); 80 | } 81 | 82 | public BatteryStatsWindow.with_parent(Window parent) { 83 | set_transient_for(parent); 84 | set_modal(true); 85 | init_window(); 86 | init_window_for_parent(); 87 | } 88 | 89 | public void init_window () { 90 | define_colors(); 91 | 92 | title = "Aptik Battery Monitor" + " v" + AppVersion; 93 | window_position = WindowPosition.CENTER; 94 | resizable = true; 95 | set_default_size (def_width, def_height); 96 | 97 | //vbox_main 98 | vbox_main = new Box (Orientation.VERTICAL, 6); 99 | vbox_main.margin = 6; 100 | add (vbox_main); 101 | 102 | init_header(); 103 | 104 | init_graph(); 105 | 106 | init_stats(); 107 | 108 | //TODO: Should start if not already running 109 | 110 | show_all(); 111 | 112 | timer_refresh_graph(); 113 | 114 | timer_refresh = Timeout.add(30 * 1000, timer_refresh_graph); 115 | } 116 | 117 | public void define_colors(){ 118 | 119 | color_white = Gdk.RGBA(); 120 | color_white.parse("white"); 121 | color_white.alpha = 1.0; 122 | 123 | color_black = Gdk.RGBA(); 124 | color_black.parse("black"); 125 | color_black.alpha = 1.0; 126 | 127 | color_red = Gdk.RGBA(); 128 | color_red.parse("red"); 129 | color_red.alpha = 1.0; 130 | 131 | color_blue = Gdk.RGBA(); 132 | color_blue.parse("blue"); 133 | color_blue.alpha = 1.0; 134 | 135 | color_red_100 = Gdk.RGBA(); 136 | color_red_100.parse("#FFCDD2"); 137 | color_red_100.alpha = 1.0; 138 | } 139 | 140 | public void init_window_for_parent() { 141 | //btn_actions.hide(); 142 | //btn_selections.hide(); 143 | } 144 | 145 | public void init_header() { 146 | 147 | Gtk.Frame frame_top = new Gtk.Frame (""); 148 | //(frame_stats_line.label_widget as Gtk.Label).use_markup = true; 149 | vbox_main.add (frame_top); 150 | 151 | var grid_top = new Grid(); 152 | grid_top.set_column_spacing (6); 153 | grid_top.set_row_spacing (6); 154 | grid_top.margin_left = 12; 155 | grid_top.margin_right = 6; 156 | grid_top.margin_bottom = 6; 157 | frame_top.add(grid_top); 158 | 159 | lbl_stats_line_batt = new Label(""); 160 | lbl_stats_line_batt.xalign = (float) 0.0; 161 | grid_top.attach(lbl_stats_line_batt, 0, 0, 1, 1); 162 | 163 | lbl_stats_line_used = new Label(""); 164 | lbl_stats_line_used.xalign = (float) 0.0; 165 | grid_top.attach(lbl_stats_line_used, 0, 1, 1, 1); 166 | 167 | var lbl_spacer = new Label(""); 168 | lbl_spacer.hexpand = true; 169 | grid_top.attach(lbl_spacer, 1, 0, 1, 1); 170 | 171 | /* //btn_settings 172 | var btn_settings = new Gtk.Button.from_stock ("gtk-missing-image"); 173 | btn_settings.label = _("Settings"); 174 | //btn_settings.label = ""; 175 | btn_settings.set_tooltip_text (_("Settings")); 176 | //btn_settings.image = get_shared_icon("gnome-settings","config.svg",16); 177 | //btn_settings.always_show_image = true; 178 | //btn_settings.image_position = PositionType.RIGHT; 179 | //hbox_top_line1.add(btn_settings); 180 | 181 | btn_settings.clicked.connect(() => { 182 | var dialog = new SettingsWindow(); 183 | dialog.set_transient_for(this); 184 | dialog.show_all(); 185 | dialog.run(); 186 | dialog.destroy(); 187 | });*/ 188 | 189 | //btn_donate 190 | var btn_donate = new Gtk.Button.from_stock ("gtk-missing-image"); 191 | btn_donate.label = _("Donate"); 192 | btn_donate.label = ""; 193 | btn_donate.set_tooltip_text (_("Donate")); 194 | btn_donate.image = get_shared_icon("donate","donate.svg",24); 195 | btn_donate.always_show_image = true; 196 | btn_donate.image_position = PositionType.RIGHT; 197 | grid_top.attach(btn_donate, 2, 0, 1, 2); 198 | 199 | btn_donate.clicked.connect(() => { 200 | var dialog = new DonationWindow(); 201 | dialog.set_transient_for(this); 202 | dialog.show_all(); 203 | dialog.run(); 204 | dialog.destroy(); 205 | }); 206 | 207 | //btn_about 208 | var btn_about = new Gtk.Button.from_stock ("gtk-about"); 209 | btn_about.label = _("Info"); 210 | btn_about.label = ""; 211 | btn_about.set_tooltip_text (_("Application Info")); 212 | btn_about.image = get_shared_icon("gtk-about","help-info.svg",24); 213 | btn_about.always_show_image = true; 214 | btn_about.image_position = PositionType.RIGHT; 215 | grid_top.attach(btn_about, 3, 0, 1, 2); 216 | 217 | btn_about.clicked.connect (btn_about_clicked); 218 | } 219 | 220 | private void btn_about_clicked () { 221 | 222 | var dialog = new AboutWindow(); 223 | dialog.set_transient_for (this); 224 | 225 | dialog.authors = { 226 | "Tony George:teejeetech@gmail.com" 227 | }; 228 | 229 | dialog.translators = { 230 | //"giulux (Italian)", 231 | //"Jorge Jamhour (Brazilian Portuguese):https://launchpad.net/~jorge-jamhour", 232 | //"B. W. Knight (Korean):https://launchpad.net/~kbd0651", 233 | //"Rodion R. (Russian):https://launchpad.net/~r0di0n" 234 | }; 235 | 236 | dialog.documenters = null; 237 | dialog.artists = null; 238 | dialog.donations = null; 239 | 240 | dialog.program_name = AppName; 241 | dialog.comments = _(" A Battery Monitoring Utility for Laptops"); 242 | dialog.copyright = "Copyright © 2012-2018 Tony George (%s)".printf(AppAuthorEmail); 243 | dialog.version = AppVersion; 244 | dialog.logo = get_app_icon(128); 245 | 246 | dialog.license = "This program is free for personal and commercial use and comes with absolutely no warranty. You use this program entirely at your own risk. The author will not be liable for any damages arising from the use of this program."; 247 | dialog.website = "http://teejeetech.in"; 248 | dialog.website_label = "http://teejeetech.blogspot.in"; 249 | 250 | dialog.initialize(); 251 | dialog.show_all(); 252 | } 253 | 254 | public void init_graph() { 255 | 256 | drawing_area = new Gtk.DrawingArea(); 257 | drawing_area.set_size_request(400, 200); 258 | drawing_area.margin = 10; 259 | 260 | var sw_graph = new ScrolledWindow(null, null); 261 | sw_graph.set_shadow_type (ShadowType.ETCHED_IN); 262 | sw_graph.expand = true; 263 | 264 | sw_graph.add (drawing_area); 265 | vbox_main.add(sw_graph); 266 | 267 | drawing_area.add_events(Gdk.EventMask.BUTTON_PRESS_MASK); 268 | 269 | /*drawing_area.button_press_event.connect((event) => { 270 | redraw_graph_area(); 271 | return true; 272 | });*/ 273 | 274 | //this.add_events(Gdk.EventMask.POINTER_MOTION_MASK); 275 | drawing_area.add_events(Gdk.EventMask.POINTER_MOTION_MASK); 276 | 277 | drawing_area.motion_notify_event.connect((event)=>{ 278 | string msg = ""; 279 | BatteryStat stat_last = null; 280 | foreach(var stat in App.battery_stats_list) { 281 | if (stat.date == null) { 282 | continue; 283 | } 284 | 285 | if (Math.fabsf((float)(stat.graph_x - event.x)) < X_INTERVAL) { 286 | msg += _("Date") + ": %s\n".printf(stat.date.format("%d %b %Y, %I:%M %p")); 287 | msg += _("Battery") + ": %0.2f %%, %0.0f mAh, %0.2f Wh, %0.2f V\n".printf( 288 | stat.charge_percent(), 289 | stat.charge_in_mah(), 290 | stat.charge_in_wh(), 291 | stat.voltage() 292 | ); 293 | msg += _("CPU") + ": %0.2f %%".printf(stat.cpu_percent()); 294 | break; 295 | } 296 | 297 | stat_last = stat; 298 | } 299 | 300 | drawing_area.set_tooltip_text(msg); 301 | 302 | return true; 303 | }); 304 | 305 | drawing_area.draw.connect ((context) => { 306 | //weak Gtk.StyleContext style_context = drawing_area.get_style_context (); 307 | //var color_default = style_context.get_color (0); 308 | 309 | var color_default = color_black; 310 | 311 | Gdk.cairo_set_source_rgba (context, color_default); 312 | context.set_line_width (1); 313 | 314 | int stat_count = (App.battery_stats_list.size > 2880) ? 2880 : App.battery_stats_list.size; 315 | 316 | //int w = drawing_area.get_allocated_width(); 317 | int h = drawing_area.get_allocated_height(); 318 | 319 | //int w_eff = w - X_OFFSET; 320 | int h_eff = h - 2 * Y_OFFSET - 10; 321 | 322 | //double pxw = (w_eff / 100.00); 323 | double pxh = (h_eff / 100.00); 324 | 325 | long x0 = X_OFFSET; 326 | long y0 = h - Y_OFFSET - 10; 327 | long x100 = x0 + (stat_count * X_INTERVAL); 328 | long y100 = Y_OFFSET; 329 | 330 | long x, x_prev, y, y_prev, y_cpu, y_prev_cpu; 331 | x = x_prev = x0; 332 | y = y_prev = y_cpu = y_prev_cpu = y0; 333 | 334 | int count = 0; 335 | BatteryStat stat_prev = null; 336 | int count_24_hours = 2880; 337 | 338 | int x_interval_counter = 0; 339 | foreach(var stat in App.battery_stats_list) { 340 | if (stat.date == null) { 341 | continue; 342 | } 343 | count++; 344 | 345 | if (App.battery_stats_list.size > count_24_hours) { 346 | if (count < (App.battery_stats_list.size - 2880)) { 347 | //ignore entries older than latest 2880 records 348 | continue; 349 | } 350 | } 351 | 352 | x += X_INTERVAL; 353 | stat.graph_x = x; 354 | 355 | if (stat_prev != null) { 356 | if (stat_prev.date.add_seconds(Main.BATT_STATS_LOG_INTERVAL + 1).compare(stat.date) < 0) { 357 | 358 | //draw double-vertical lines to indicate time gap 359 | 360 | //------ BEGIN CONTEXT ------------------------------------------- 361 | context.set_line_width (0.5); 362 | Gdk.cairo_set_source_rgba (context, color_default); 363 | 364 | //draw a vertical line 365 | context.move_to(x_prev, y0); 366 | context.line_to(x_prev, y100); 367 | 368 | //draw a vertical line 369 | context.move_to(x, y0); 370 | context.line_to(x, y100); 371 | 372 | context.stroke (); 373 | //------ END CONTEXT ----------------------------------------- 374 | } 375 | else { 376 | //------ BEGIN CONTEXT ------------------------------------------- 377 | context.set_line_width (0.5); 378 | Gdk.cairo_set_source_rgba (context, color_default); 379 | 380 | for (int p = 10; p <= 100; p += 10) { 381 | long y_interval = (long) (h - Y_OFFSET - 10 - (p * pxh)); 382 | //draw Y-axis lines 383 | context.move_to(x_prev, y_interval); 384 | context.line_to(x, y_interval); 385 | } 386 | 387 | context.stroke (); 388 | //------ END CONTEXT --------------------------------------------- 389 | 390 | //------ BEGIN CONTEXT ------------------------------------------- 391 | context.set_line_width (1); 392 | Gdk.cairo_set_source_rgba (context, color_default); 393 | 394 | //draw battery line for stat --------------------------- 395 | 396 | context.move_to (x_prev, y_prev); 397 | 398 | y = (long) ((stat.charge_now * 100.00 * pxh) / BatteryStat.batt_charge_full()); 399 | y = h - Y_OFFSET - 10 - y; 400 | 401 | context.line_to (x, y); 402 | 403 | context.stroke (); 404 | //------ END CONTEXT --------------------------------------------- 405 | 406 | //------ BEGIN CONTEXT ------------------------------------------- 407 | context.set_line_width (1); 408 | Gdk.cairo_set_source_rgba (context, color_red); 409 | 410 | //draw cpu line for stat ---------------- 411 | 412 | context.move_to (x_prev, y_prev_cpu); 413 | 414 | y_cpu = (long) (stat.cpu_percent() * pxh); 415 | y_cpu = h - Y_OFFSET - 10 - y_cpu; 416 | context.line_to (x, y_cpu); 417 | 418 | y_prev_cpu = y_cpu; 419 | 420 | context.stroke (); 421 | //------ END CONTEXT --------------------------------------------- 422 | } 423 | } 424 | 425 | if ((stat_current != null) && (stat_current.date == stat.date)) { 426 | //------ BEGIN CONTEXT ------------------------------------------- 427 | context.set_line_width (0.5); 428 | Gdk.cairo_set_source_rgba (context, color_blue); 429 | //draw a vertical line for selected stat 430 | context.move_to(x, y0); 431 | context.line_to(x, y100); 432 | context.stroke (); 433 | //------ END CONTEXT --------------------------------------------- 434 | } 435 | 436 | //------ BEGIN CONTEXT ------------------------------------------------- 437 | context.set_line_width (1); 438 | Gdk.cairo_set_source_rgba (context, color_default); 439 | 440 | //draw X-axis ticks and time label for stat ----------------- 441 | 442 | x_interval_counter++; 443 | 444 | if (((stat.date.get_minute() % 10) == 0) && (stat.date.get_second() < 30)) { 445 | //draw X-axis tick 446 | context.move_to(x, y0 - 2); 447 | context.line_to(x, y0 + 2); 448 | 449 | if (x_interval_counter >= 20){ 450 | //draw time on X-axis tick 451 | context.move_to (x - 15, h - Y_OFFSET - 10 + 20); 452 | context.show_text(stat.date.format("%I:%M %p")); 453 | } 454 | 455 | x_interval_counter = 0; 456 | } 457 | 458 | context.stroke (); 459 | //------ END CONTEXT --------------------------------------------------- 460 | 461 | stat_prev = stat; 462 | if (stat_current == null) { 463 | stat_current = stat; 464 | } 465 | x_prev = x; 466 | y_prev = y; 467 | } 468 | 469 | //------ BEGIN CONTEXT ------------------------------------------------- 470 | 471 | context.set_line_width (1); 472 | Gdk.cairo_set_source_rgba (context, color_default); 473 | 474 | //draw axis lines ----------------------------- 475 | 476 | //draw X-axis line 477 | context.move_to(x0, y0); 478 | context.line_to(x100, y0); 479 | 480 | //draw Y-axis line 481 | context.move_to(x0, y0); 482 | context.line_to(x0, y100); 483 | 484 | context.stroke (); 485 | 486 | //------ END CONTEXT --------------------------------------------------- 487 | 488 | //------ BEGIN CONTEXT ------------------------------------------------- 489 | context.set_line_width (0.5); 490 | 491 | //draw Y-axis markers and labels ------------------------ 492 | 493 | for (int p = 10; p <= 100; p += 10) { 494 | y = (long) (h - Y_OFFSET - 10 - (p * pxh)); 495 | 496 | //draw Y-axis markers 497 | context.move_to(x0 + 2, y); 498 | context.line_to(x0 - 2, y); 499 | 500 | /* 501 | //draw Y-axis lines 502 | context.move_to(x0, y); 503 | context.line_to(x100, y); 504 | */ 505 | 506 | //draw Y-axis labels 507 | context.move_to (x0 - X_OFFSET, y); 508 | context.show_text("%d%%".printf(p)); 509 | } 510 | 511 | context.stroke (); 512 | //------ END CONTEXT --------------------------------------------------- 513 | 514 | drawing_area.set_size_request((X_INTERVAL * stat_count) + (2 * X_OFFSET), -1); 515 | 516 | return true; 517 | }); 518 | } 519 | 520 | public void init_stats() { 521 | 522 | //frame_main 523 | Gtk.Frame frame_main = new Gtk.Frame (""); 524 | //(frame_main.label_widget as Gtk.Label).use_markup = true; 525 | vbox_main.add (frame_main); 526 | 527 | var grid_main = new Grid(); 528 | grid_main.set_column_spacing (18); 529 | grid_main.set_row_spacing (6); 530 | grid_main.margin_left = 18; 531 | grid_main.margin_bottom = 6; 532 | frame_main.add(grid_main); 533 | 534 | int col = -1; 535 | 536 | //percent 537 | lbl_percent = new Label(_("Battery")); 538 | grid_main.attach(lbl_percent, ++col, 1, 1, 1); 539 | 540 | lbl_percent_val = new Label("" + _("0h 0m") + ""); 541 | lbl_percent_val.set_use_markup(true); 542 | grid_main.attach(lbl_percent_val, col, 0, 1, 1); 543 | 544 | //avg_life 545 | lbl_avg_life = new Label(_("Average Life")); 546 | lbl_avg_life.set_no_show_all(true); 547 | grid_main.attach(lbl_avg_life, ++col, 1, 1, 1); 548 | 549 | lbl_avg_life_val = new Label("" + _("0h 0m") + ""); 550 | lbl_avg_life_val.set_use_markup(true); 551 | lbl_avg_life_val.set_no_show_all(true); 552 | grid_main.attach(lbl_avg_life_val, col, 0, 1, 1); 553 | 554 | //remaining 555 | lbl_remaining = new Label(_("Remaining")); 556 | lbl_remaining.set_no_show_all(true); 557 | grid_main.attach(lbl_remaining, ++col, 1, 1, 1); 558 | 559 | lbl_remaining_val = new Label("" + _("0h 0m") + ""); 560 | lbl_remaining_val.set_use_markup(true); 561 | lbl_remaining_val.set_no_show_all(true); 562 | grid_main.attach(lbl_remaining_val, col, 0, 1, 1); 563 | 564 | //remaining_to_charge 565 | lbl_time_to_charge = new Label(_("To Full Charge")); 566 | lbl_time_to_charge.set_no_show_all(true); 567 | grid_main.attach(lbl_time_to_charge, ++col, 1, 1, 1); 568 | 569 | lbl_time_to_charge_val = new Label("" + _("0h 0m") + ""); 570 | lbl_time_to_charge_val.set_use_markup(true); 571 | lbl_time_to_charge_val.set_no_show_all(true); 572 | grid_main.attach(lbl_time_to_charge_val, col, 0, 1, 1); 573 | 574 | //rate 575 | lbl_rate = new Label(_("Per Hour")); 576 | lbl_rate.set_no_show_all(true); 577 | grid_main.attach(lbl_rate, ++col, 1, 1, 1); 578 | 579 | lbl_rate_val = new Label("" + _("0.0%") + ""); 580 | lbl_rate_val.set_use_markup(true); 581 | lbl_rate_val.set_no_show_all(true); 582 | grid_main.attach(lbl_rate_val, col, 0, 1, 1); 583 | 584 | //health 585 | lbl_health = new Label(_("Health")); 586 | lbl_health.set_no_show_all(true); 587 | grid_main.attach(lbl_health, ++col, 1, 1, 1); 588 | 589 | lbl_health_val = new Label("" + _("0.0%") + ""); 590 | lbl_health_val.set_use_markup(true); 591 | lbl_health_val.set_no_show_all(true); 592 | grid_main.attach(lbl_health_val, col, 0, 1, 1); 593 | 594 | //spacer 595 | var lbl_spacer = new Label(""); 596 | lbl_spacer.hexpand = true; 597 | grid_main.attach(lbl_spacer, ++col, 0, 1, 2); 598 | 599 | //battery icon 600 | img_battery_status = get_shared_icon("notification-battery-060", "notification-battery-060.png", 80); 601 | img_battery_status.margin_right = 5; 602 | grid_main.attach(img_battery_status, ++col, 0, 1, 2); 603 | } 604 | 605 | public bool timer_refresh_graph() { 606 | 607 | //if (timer_pkg_info > 0){ 608 | // Source.remove(timer_pkg_info); 609 | // timer_pkg_info = 0; 610 | //} 611 | 612 | App.read_battery_stats(); 613 | select_latest_stat(); 614 | update_info_current_cycle(); 615 | update_battery_status_icon(); 616 | return true; 617 | } 618 | 619 | private void select_latest_stat(){ 620 | 621 | if (App.battery_stats_list.size >= 2){ 622 | var stat0 = App.battery_stats_list[App.battery_stats_list.size - 1]; 623 | var stat1 = App.battery_stats_list[App.battery_stats_list.size - 2]; 624 | stat_current = stat0; 625 | redraw_graph_area(); 626 | update_info_stats(stat0,stat1); 627 | } 628 | } 629 | 630 | private void redraw_graph_area() { 631 | drawing_area.queue_draw_area(0, 0, 632 | drawing_area.get_allocated_width(), 633 | drawing_area.get_allocated_height()); 634 | } 635 | 636 | private void update_info_stats(BatteryStat stat, BatteryStat stat_prev){ 637 | lbl_stats_line_batt.label = _("Battery") + ": %0.2f %%, %0.0f mAh, %0.2f Wh, %0.2f V".printf( 638 | stat.charge_percent(), 639 | stat.charge_in_mah(), 640 | stat.charge_in_wh(), 641 | stat.voltage() 642 | ); 643 | } 644 | 645 | private void update_info_current_cycle(){ 646 | 647 | if (App.battery_stats_list.size < 2){ return; } 648 | 649 | var cycle = new BatteryCycle(); 650 | cycle.calculate_stats(App.battery_stats_list); 651 | 652 | //var stat_first = App.battery_stats_list[0]; 653 | var stat_prev = App.battery_stats_list[App.battery_stats_list.size - 2]; 654 | var stat_current = App.battery_stats_list[App.battery_stats_list.size - 1]; 655 | 656 | lbl_stats_line_used.label = _("Used") + ": " + cycle.used_string(); 657 | 658 | lbl_percent_val.label = format_label("%.0f%%".printf(stat_current.charge_percent())); 659 | 660 | if (BatteryStat.is_charging()){ 661 | 662 | //charging ------------------------------ 663 | 664 | lbl_avg_life.visible = lbl_avg_life_val.visible = false; 665 | lbl_remaining.visible = lbl_remaining_val.visible = false; 666 | lbl_time_to_charge.visible = lbl_time_to_charge_val.visible = true; 667 | lbl_rate.visible = lbl_rate_val.visible = true; 668 | lbl_health.visible = lbl_health_val.visible = true; 669 | 670 | double rate = (stat_current.charge_percent() - stat_prev.charge_percent()) * 2; 671 | double estimated = (100 - stat_current.charge_percent()) / rate; 672 | 673 | if (estimated > 0){ 674 | lbl_time_to_charge_val.label = format_label(BatteryCycle.mins_to_string(estimated)); 675 | } 676 | else{ 677 | lbl_time_to_charge_val.label = format_label("??"); 678 | } 679 | 680 | if (estimated > 0){ 681 | lbl_rate_val.label = format_label("+%0.0f%%".printf(rate * 60.0)); 682 | } 683 | else{ 684 | lbl_rate_val.label = format_label("??"); 685 | } 686 | 687 | if (BatteryStat.batt_health() > 0){ 688 | lbl_health_val.label = format_label("%0.0f%%".printf(BatteryStat.batt_health())); 689 | } 690 | else{ 691 | lbl_health_val.label = format_label("??"); 692 | } 693 | } 694 | else{ 695 | 696 | //discharging ------------------------------ 697 | 698 | lbl_avg_life.visible = lbl_avg_life_val.visible = true; 699 | lbl_remaining.visible = lbl_remaining_val.visible = true; 700 | lbl_time_to_charge.visible = lbl_time_to_charge_val.visible = false; 701 | lbl_rate.visible = lbl_rate_val.visible = true; 702 | lbl_health.visible = lbl_health_val.visible = true; 703 | 704 | double rate = (stat_prev.charge_percent() - stat_current.charge_percent()) * 2; 705 | double estimated = 100 / rate; 706 | 707 | lbl_avg_life_val.label = format_label(cycle.battery_life_string()); 708 | 709 | if (estimated > 0){ 710 | lbl_remaining_val.label = format_label(cycle.remaining_time_string()); 711 | } 712 | else{ 713 | lbl_remaining_val.label = format_label("??"); 714 | } 715 | 716 | if (rate > 0){ 717 | lbl_rate_val.label = format_label("-%0.0f%%".printf(cycle.drop_per_min * 60.0)); 718 | } 719 | else{ 720 | lbl_rate_val.label = format_label("??"); 721 | } 722 | } 723 | 724 | if (BatteryStat.batt_health() > 0){ 725 | lbl_health_val.label = format_label("%0.0f%%".printf(BatteryStat.batt_health())); 726 | } 727 | else{ 728 | lbl_health_val.label = format_label("??"); 729 | } 730 | } 731 | 732 | private string format_label(string text){ 733 | return "" + text + ""; 734 | } 735 | 736 | private void update_battery_status_icon(){ 737 | 738 | var icon_name = "notification-battery"; 739 | 740 | var stat = new BatteryStat.read_from_sys(); 741 | double percent = stat.charge_percent(); 742 | 743 | if (percent == 100){ 744 | icon_name += "-100"; 745 | } 746 | else if (percent >= 80){ 747 | icon_name += "-080"; 748 | } 749 | else if (percent >= 60){ 750 | icon_name += "-060"; 751 | } 752 | else if (percent >= 40){ 753 | icon_name += "-040"; 754 | } 755 | else if (percent >= 20){ 756 | icon_name += "-020"; 757 | } 758 | else if (percent >= 0){ 759 | icon_name += "-000"; 760 | } 761 | 762 | if (BatteryStat.is_charging()){ 763 | icon_name += "-plugged"; 764 | } 765 | 766 | var img = get_shared_icon(icon_name, "%s.png".printf(icon_name), 48); 767 | if (img != null){ 768 | img_battery_status.set_from_pixbuf(img.pixbuf); 769 | } 770 | } 771 | } 772 | 773 | -------------------------------------------------------------------------------- /src/DiskIndicator.vala: -------------------------------------------------------------------------------- 1 | 2 | using GLib; 3 | using Gtk; 4 | using Gee; 5 | using Json; 6 | 7 | using TeeJee.Logging; 8 | using TeeJee.FileSystem; 9 | using TeeJee.JsonHelper; 10 | using TeeJee.ProcessHelper; 11 | using TeeJee.GtkHelper; 12 | using TeeJee.System; 13 | using TeeJee.Misc; 14 | 15 | using AppIndicator; 16 | 17 | public class DiskIndicator: GLib.Object{ 18 | 19 | protected Gee.ArrayList device_list; 20 | protected Indicator indicator; 21 | protected string icon; 22 | protected string name; 23 | protected DateTime last_refresh_date = null; 24 | 25 | public DiskIndicator(){ 26 | 27 | App.disk_indicator = this; 28 | 29 | this.name = "Disk Manager"; 30 | this.icon = "disks"; 31 | this.indicator = new Indicator( 32 | "indicator_diskman", icon, IndicatorCategory.APPLICATION_STATUS); 33 | 34 | //indicator.set_status(IndicatorStatus.ACTIVE); 35 | 36 | 37 | indicator.set_status(IndicatorStatus.ACTIVE); 38 | 39 | refresh_tray_icon(); 40 | 41 | refresh_device_list(); 42 | 43 | var menu = new Gtk.Menu(); 44 | 45 | // open ------------------------------------- 46 | 47 | var item = new Gtk.ImageMenuItem.with_label(_("Open")); 48 | menu.append(item); 49 | var item_open = item; 50 | 51 | item.always_show_image = true; 52 | item.set_image(get_shared_icon("inode-directory","locked.png",16)); 53 | item.set_reserve_indicator(false); 54 | 55 | item.activate.connect(()=>{ 56 | var submenu = get_menu("open"); 57 | item_open.set_submenu(submenu); 58 | }); 59 | 60 | item.activate(); 61 | 62 | // eject ------------------------------------- 63 | 64 | /* 65 | item = new Gtk.MenuItem.with_label(_("Eject")); 66 | menu.append(item); 67 | var item_eject = item; 68 | 69 | item.activate.connect(()=>{ 70 | var submenu = get_menu("eject"); 71 | item_eject.set_submenu(submenu); 72 | }); 73 | 74 | item.activate(); 75 | */ 76 | 77 | // mount ------------------------------------- 78 | 79 | var separator = new Gtk.SeparatorMenuItem (); 80 | menu.add (separator); 81 | 82 | item = new Gtk.ImageMenuItem.with_label(_("Mount")); 83 | menu.append(item); 84 | var item_mount = item; 85 | 86 | item.always_show_image = true; 87 | item.set_image(get_shared_icon("","drive-harddisk.svg",16)); 88 | item.set_reserve_indicator(false); 89 | 90 | item.activate.connect(()=>{ 91 | var submenu = get_menu("mount"); 92 | item_mount.set_submenu(submenu); 93 | }); 94 | 95 | item.activate(); 96 | 97 | // unmount ------------------------------------- 98 | 99 | item = new Gtk.ImageMenuItem.with_label(_("Unmount")); 100 | menu.append(item); 101 | var item_unmount = item; 102 | 103 | item.always_show_image = true; 104 | item.set_image(get_shared_icon("media-eject","",16)); 105 | item.set_reserve_indicator(false); 106 | 107 | item.activate.connect(()=>{ 108 | var submenu = get_menu("unmount"); 109 | item_unmount.set_submenu(submenu); 110 | }); 111 | 112 | item.activate(); 113 | 114 | // lock ------------------------------------- 115 | 116 | separator = new Gtk.SeparatorMenuItem (); 117 | menu.add (separator); 118 | 119 | item = new Gtk.ImageMenuItem.with_label(_("Lock")); 120 | menu.append(item); 121 | var item_lock = item; 122 | 123 | item.always_show_image = true; 124 | item.set_image(get_shared_icon("","locked.png",16)); 125 | item.set_reserve_indicator(false); 126 | 127 | item.activate.connect(()=>{ 128 | var submenu = get_menu("lock"); 129 | item_lock.set_submenu(submenu); 130 | }); 131 | 132 | item.activate(); 133 | 134 | // unlock ------------------------------------- 135 | 136 | item = new Gtk.ImageMenuItem.with_label(_("Unlock")); 137 | menu.append(item); 138 | var item_unlock = item; 139 | 140 | item.always_show_image = true; 141 | item.set_image(get_shared_icon("","unlocked.png",16)); 142 | 143 | item.activate.connect(()=>{ 144 | var submenu = get_menu("unlock"); 145 | item_unlock.set_submenu(submenu); 146 | }); 147 | 148 | item.activate(); 149 | 150 | // usage ------------------------------------- 151 | 152 | separator = new Gtk.SeparatorMenuItem (); 153 | menu.add (separator); 154 | 155 | item = new Gtk.ImageMenuItem.with_label(_("Usage")); 156 | menu.append(item); 157 | var item_usage = item; 158 | 159 | item.always_show_image = true; 160 | item.set_image(get_shared_icon("disk-usage-analyzer","disk-usage-analyzer.svg",16)); 161 | 162 | item.activate.connect(()=>{ 163 | var submenu = get_menu("usage"); 164 | item_usage.set_submenu(submenu); 165 | }); 166 | 167 | item.activate(); 168 | 169 | // iso ------------------------------------- 170 | 171 | separator = new Gtk.SeparatorMenuItem (); 172 | menu.add (separator); 173 | 174 | item = new Gtk.ImageMenuItem.with_label(_("ISO")); 175 | menu.append(item); 176 | var item_iso = item; 177 | 178 | item.always_show_image = true; 179 | item.set_image(get_shared_icon("media-optical","media-optical.png",16)); 180 | 181 | item.activate.connect(()=>{ 182 | var submenu = get_menu("iso"); 183 | item_iso.set_submenu(submenu); 184 | }); 185 | 186 | item.activate(); 187 | 188 | // settings ------------------------------------- 189 | 190 | separator = new Gtk.SeparatorMenuItem (); 191 | menu.add (separator); 192 | 193 | item = new Gtk.ImageMenuItem.with_label(_("Settings")); 194 | menu.append(item); 195 | var item_settings = item; 196 | 197 | item.always_show_image = true; 198 | item.set_image(get_shared_icon("preferences-system","settings.png",16)); 199 | 200 | item.activate.connect(()=>{ 201 | var dlg = new SettingsWindow(null); 202 | }); 203 | 204 | //item.activate(); 205 | 206 | // about ------------------------------------- 207 | 208 | separator = new Gtk.SeparatorMenuItem (); 209 | menu.add (separator); 210 | 211 | item = new Gtk.ImageMenuItem.with_label(_("About")); 212 | menu.append(item); 213 | 214 | item.activate.connect(() => { 215 | btn_about_clicked(); 216 | }); 217 | 218 | // donate ------------------------------------- 219 | 220 | item = new Gtk.ImageMenuItem.with_label(_("Donate")); 221 | menu.append(item); 222 | 223 | item.activate.connect(() => { 224 | btn_donate_clicked(); 225 | }); 226 | 227 | // exit ------------------------------------- 228 | 229 | item = new Gtk.ImageMenuItem.with_label(_("Exit")); 230 | menu.append(item); 231 | 232 | item.activate.connect(() => { 233 | App.exit_app(); 234 | exit(0); 235 | }); 236 | 237 | indicator.set_menu(menu); 238 | menu.show_all(); 239 | } 240 | 241 | public void refresh_tray_icon(){ 242 | if (App.use_custom_tray_icon && file_exists(App.custom_tray_icon_path)){ 243 | log_msg("Setting custom tray icon"); 244 | indicator.set_icon_theme_path(file_parent(App.custom_tray_icon_path)); 245 | indicator.set_icon(file_basename(App.custom_tray_icon_path).replace(".png","").replace(".svg","").replace(".jpg","").replace(".jpeg","")); 246 | } 247 | else{ 248 | this.icon = "disks"; 249 | indicator.set_icon_theme_path("/usr/share/%s/images".printf(AppShortName)); 250 | indicator.set_icon("disks"); 251 | } 252 | } 253 | 254 | private void refresh_device_list_if_stale(){ 255 | var period = (new DateTime.now_local()).add_seconds(-10); 256 | if ((device_list == null) || (last_refresh_date == null) || (last_refresh_date.compare(period) < 0)){ 257 | device_list = Device.get_block_devices(true); 258 | last_refresh_date = new DateTime.now_local(); 259 | } 260 | } 261 | 262 | private void refresh_device_list(){ 263 | device_list = Device.get_block_devices(true); 264 | last_refresh_date = new DateTime.now_local(); 265 | } 266 | 267 | private Gtk.Menu get_menu (string action = "") { 268 | 269 | //log_debug("get_menu(\"%s\")".printf(action)); 270 | 271 | refresh_device_list_if_stale(); 272 | 273 | var menu = new Gtk.Menu(); 274 | menu.reserve_toggle_size = false; 275 | 276 | var dummy_window = new Gtk.Window(); 277 | 278 | for(int i=0; i < device_list.size; i++){ 279 | 280 | var dev = device_list[i]; 281 | 282 | if (dev.size_bytes < 10 * KB){ 283 | continue; 284 | } 285 | 286 | switch(action){ 287 | case "open": 288 | bool show = (dev.type == "disk") || (dev.type == "loop") || !dev.is_encrypted_partition() || !dev.has_children(); 289 | if (!show){ 290 | continue; 291 | } 292 | break; 293 | case "eject": 294 | bool show = ((dev.type == "disk") || (dev.type == "loop")) && (dev.removable); 295 | if (!show){ 296 | continue; 297 | } 298 | break; 299 | case "mount": 300 | bool show = (dev.type == "disk") || ((dev.mount_points.size == 0) && (!dev.is_encrypted_partition() || !dev.has_children())); 301 | if (!show){ 302 | continue; 303 | } 304 | break; 305 | case "unmount": 306 | case "usage": 307 | bool show = (dev.type == "disk") 308 | || ((dev.type == "loop") && dev.has_children()) 309 | || (dev.mount_points.size > 0); 310 | if (!show){ 311 | continue; 312 | } 313 | break; 314 | case "lock": 315 | bool show = (dev.type == "disk") || (dev.is_on_encrypted_partition()); 316 | if (!show){ 317 | continue; 318 | } 319 | break; 320 | case "unlock": 321 | bool show = (dev.type == "disk") || (dev.is_encrypted_partition() && !dev.has_children()); 322 | if (!show){ 323 | continue; 324 | } 325 | break; 326 | 327 | case "iso": 328 | continue; 329 | } 330 | 331 | Gtk.Image icon = null; 332 | 333 | //if (action != "usage"){ 334 | if ((dev.type == "crypt") && (dev.pkname.length > 0)){ 335 | icon = get_shared_icon("","unlocked.png",16); 336 | } 337 | else if (dev.fstype.contains("luks")){ 338 | icon = get_shared_icon("","locked.png",16); 339 | } 340 | else if (dev.fstype.contains("iso9660") || (dev.type == "loop")){ 341 | icon = get_shared_icon("media-optical","media-optical.png",16); 342 | } 343 | else{ 344 | icon = get_shared_icon("","drive-harddisk.svg",16); 345 | } 346 | //} 347 | 348 | var name = ""; 349 | 350 | switch(action){ 351 | case "usage": 352 | 353 | if ((dev.type == "disk") || ((dev.type == "loop") && dev.has_children())){ 354 | name += "%s".printf(dev.description_simple()); 355 | break; 356 | } 357 | 358 | name += "%s ~ %s".printf(dev.short_name_with_parent, dev.description_usage()); 359 | 360 | if ((dev.used_bytes > 0) && (dev.size_bytes > 0)){ 361 | double usage_percent = (dev.used_bytes * 10.0) / dev.size_bytes; 362 | int used_count = (int) usage_percent; 363 | int free_count = 10 - used_count; 364 | 365 | name += " "; 366 | for(int j = 0; j < used_count; j++){ 367 | name += "▮"; 368 | } 369 | for(int j = 0; j < free_count; j++){ 370 | name += "▯"; 371 | } 372 | } 373 | else{ 374 | name += " "; 375 | for(int j = 0; j < 10; j++){ 376 | //name += "░";░ ▒ ▓ ⬜ ⬛ ▯ ▮ 377 | } 378 | } 379 | 380 | break; 381 | 382 | default: 383 | name += "%s".printf(dev.description_simple()); 384 | break; 385 | } 386 | 387 | if ((dev.type != "disk") && ((action == "open") || (action == "unmount"))){ 388 | if (dev.mount_points.size > 0){ 389 | name += " ~ " + dev.mount_points[0].mount_point; 390 | } 391 | } 392 | 393 | var item = new Gtk.ImageMenuItem.with_label (name); 394 | item.set_reserve_indicator(false); 395 | menu.append(item); 396 | 397 | if (((dev.type == "loop") && dev.has_parent()) || (dev.type == "crypt") || (dev.type == "part") || (dev.type == "lvm")){ 398 | item.always_show_image = true; 399 | item.set_image(icon); 400 | } 401 | 402 | /*foreach(var child in gtk_container_get_children(item)){ 403 | //log_debug(child.get_type().to_string() + "-" + typeof(Gtk.Label).to_string()); 404 | if (child is Gtk.Label){ 405 | Gtk.Label label = (Gtk.Label) child; 406 | label.set_use_markup(true); 407 | label.set_markup(name); 408 | //log_debug("markup=%s".printf(name)); 409 | break; 410 | } 411 | }*/ 412 | 413 | // set sensitive ---------------------------------------- 414 | 415 | switch(action){ 416 | case "eject": 417 | if ((dev.type == "disk") || ((dev.type == "loop") && (dev.has_children()))){ 418 | item.sensitive = false; 419 | foreach(var child in dev.children){ 420 | if (child.has_children()){ 421 | foreach(var decendant in child.children){ 422 | if (decendant.mount_points.size > 0){ 423 | item.sensitive = true; 424 | break; 425 | } 426 | } 427 | } 428 | else{ 429 | if (child.mount_points.size > 0){ 430 | item.sensitive = true; 431 | break; 432 | } 433 | } 434 | } 435 | } 436 | break; 437 | case "open": 438 | case "usage": 439 | case "mount": 440 | case "unmount": 441 | case "lock": 442 | case "unlock": 443 | if ((dev.type == "disk") || ((dev.type == "loop") && (dev.has_children()))){ 444 | item.sensitive = false; 445 | } 446 | break; 447 | } 448 | 449 | // actions ------------------------------------------ 450 | 451 | switch(action){ 452 | case "eject": 453 | item.activate.connect(() => { 454 | 455 | foreach(var child in dev.children){ 456 | 457 | // unmount if mounted 458 | if (child.mount_points.size > 0){ 459 | bool ok = Device.unmount_udisks(child.device, dummy_window); 460 | if (!ok){ 461 | return; 462 | } 463 | } 464 | 465 | // Note: We only need to unmount. Locking LUKS devices is not required. 466 | // As long as all partitions are unmounted, the device can be safely removed 467 | } 468 | 469 | string title = "%s: %s".printf(_("Unmounted"), dev.device);; 470 | string msg = "%s\n%s".printf(_("Disk can be safely removed"), dev.description_simple()); 471 | OSDNotify.notify_send(title, msg, 2000, "normal", "info"); 472 | 473 | device_list = null; 474 | }); 475 | break; 476 | 477 | case "open": 478 | item.activate.connect(() => { 479 | 480 | if (dev.fstype == "swap"){ 481 | string title = _("Swap Device!"); 482 | string message = _("Swap devices cannot be opened in file manager"); 483 | gtk_messagebox(title, message, null, true); 484 | return; 485 | } 486 | 487 | // unlock 488 | if (dev.is_encrypted_partition()){ 489 | var dev_unlocked = luks_unlock(dev); 490 | if (dev_unlocked == null){ 491 | return; 492 | } 493 | else{ 494 | dev = dev_unlocked; 495 | } 496 | } 497 | 498 | // mount if unmounted 499 | if (dev.mount_points.size == 0){ 500 | bool ok = Device.automount_udisks(dev.device, dummy_window); 501 | if (!ok){ 502 | return; 503 | } 504 | else{ 505 | dev = dev.query_changes(); 506 | } 507 | } 508 | 509 | // browse 510 | if (dev.mount_points.size > 0){ 511 | var mp = dev.mount_points[0]; 512 | exo_open_folder(mp.mount_point); 513 | } 514 | 515 | device_list = null; 516 | }); 517 | break; 518 | 519 | case "mount": 520 | item.activate.connect(() => { 521 | 522 | // mount if unmounted 523 | if (dev.mount_points.size == 0){ 524 | bool ok = Device.automount_udisks(dev.device, dummy_window); 525 | if (!ok){ 526 | return; 527 | } 528 | else{ 529 | dev = dev.query_changes(); 530 | if (dev.mount_points.size > 0){ 531 | string title = "%s: %s".printf(_("Mounted"), dev.device); 532 | string msg = "%s".printf(dev.mount_points[0].mount_point); 533 | OSDNotify.notify_send(title, msg, 2000, "normal", "info"); 534 | } 535 | } 536 | } 537 | else{ 538 | string title = "%s: %s".printf(_("Is Mounted"), dev.device); 539 | string msg = "%s".printf(dev.mount_points[0].mount_point); 540 | OSDNotify.notify_send(title, msg, 2000, "normal", "info"); 541 | } 542 | 543 | device_list = null; 544 | }); 545 | break; 546 | 547 | case "unmount": 548 | item.activate.connect(() => { 549 | 550 | bool is_system = false; 551 | foreach (var mnt in dev.mount_points){ 552 | switch (mnt.mount_point){ 553 | case "/": 554 | case "/boot": 555 | case "/boot/efi": 556 | case "/home": 557 | is_system = true; 558 | break; 559 | default: 560 | if (dev.fstype == "swap"){ 561 | is_system = true; 562 | } 563 | break; 564 | } 565 | } 566 | 567 | if (is_system){ 568 | string title = _("System Device!"); 569 | string message = _("System devices cannot be unmounted"); 570 | gtk_messagebox(title, message, null, true); 571 | return; 572 | } 573 | 574 | // unmount if mounted 575 | if (dev.mount_points.size > 0){ 576 | bool ok = Device.unmount_udisks(dev.device, dummy_window); 577 | if (!ok){ 578 | return; 579 | } 580 | else{ 581 | string title = "%s %s".printf(_("Unmounted"), dev.device); 582 | string msg = ""; 583 | OSDNotify.notify_send(title, msg, 2000, "normal", "info"); 584 | } 585 | } 586 | else{ 587 | string title = "%s: %s".printf(_("Not mounted"), dev.device); 588 | string msg = ""; 589 | OSDNotify.notify_send(title, msg, 2000, "normal", "info"); 590 | } 591 | 592 | device_list = null; 593 | }); 594 | break; 595 | 596 | case "lock": 597 | item.activate.connect(() => { 598 | 599 | bool is_system = false; 600 | foreach (var mnt in dev.mount_points){ 601 | switch (mnt.mount_point){ 602 | case "/": 603 | case "/boot": 604 | case "/boot/efi": 605 | case "/home": 606 | is_system = true; 607 | break; 608 | default: 609 | if (dev.fstype == "swap"){ 610 | is_system = true; 611 | } 612 | break; 613 | } 614 | } 615 | 616 | if (is_system){ 617 | string title = _("System Device!"); 618 | string message = _("System devices cannot be unmounted"); 619 | gtk_messagebox(title, message, null, true); 620 | return; 621 | } 622 | 623 | // unmount if mounted 624 | if (dev.mount_points.size > 0){ 625 | bool ok = Device.unmount_udisks(dev.device, dummy_window); 626 | if (!ok){ 627 | return; 628 | } 629 | else{ 630 | dev = dev.query_changes(); 631 | } 632 | } 633 | 634 | // lock if unlocked 635 | if (dev.is_on_encrypted_partition()){ 636 | 637 | bool ok = luks_lock(dev); 638 | 639 | if (ok){ 640 | string title = "%s %s".printf(_("Locked"), dev.device); 641 | string msg = ""; 642 | OSDNotify.notify_send(title, msg, 2000, "normal", "info"); 643 | } 644 | else{ 645 | string title = _("Failed to lock"); 646 | string message = App.daemon.error_message; 647 | gtk_messagebox(title, message, null, true); 648 | } 649 | } 650 | 651 | device_list = null; 652 | }); 653 | break; 654 | 655 | case "unlock": 656 | item.activate.connect(() => { 657 | 658 | // unlock 659 | if (dev.is_encrypted_partition()){ 660 | 661 | var dev_unlocked = luks_unlock(dev); 662 | 663 | if (dev_unlocked != null){ 664 | string title = "%s %s".printf(_("Unlocked"), dev.device); 665 | string msg = "/dev/mapper/%s".printf(dev_unlocked.mapped_name); 666 | OSDNotify.notify_send(title, msg, 2000, "normal", "info"); 667 | } 668 | else{ 669 | string title = _("Failed to unlock"); 670 | string message = App.daemon.error_message; 671 | gtk_messagebox(title, message, null, true); 672 | } 673 | } 674 | 675 | device_list = null; 676 | 677 | }); 678 | break; 679 | 680 | case "usage": 681 | 682 | item.activate.connect(() => { 683 | open_baobab(dev); 684 | }); 685 | 686 | break; 687 | } 688 | } 689 | 690 | // mount iso ------------------------------------- 691 | 692 | if (action == "iso"){ 693 | 694 | var item = new Gtk.ImageMenuItem.with_label (_("Mount ISO...")); 695 | item.set_reserve_indicator(false); 696 | menu.append(item); 697 | 698 | item.always_show_image = true; 699 | var icon = get_shared_icon("media-optical","media-optical.png",16); 700 | item.set_image(icon); 701 | 702 | item.activate.connect(() => { 703 | 704 | var filters = new Gee.ArrayList(); 705 | var filter = create_file_filter("All Files", { "*" }); 706 | filters.add(filter); 707 | filter = create_file_filter("ISO Image File (*.iso)", { "*.iso" }); 708 | filters.add(filter); 709 | var default_filter = filter; 710 | 711 | var selected_files = gtk_select_files(dummy_window, true, false, filters, default_filter); 712 | string iso_file = (selected_files.size > 0) ? selected_files[0] : ""; 713 | if ((iso_file.length == 0) || !file_exists(iso_file)){ 714 | return; 715 | } 716 | 717 | var loop_dev = Device.automount_udisks_iso(iso_file, dummy_window); 718 | 719 | if (loop_dev != null){ 720 | 721 | // notify 722 | string title = "%s".printf(_("Mounted ISO File")); 723 | string msg = "%s".printf(loop_dev.device); 724 | OSDNotify.notify_send(title, msg, 2000, "normal", "info"); 725 | 726 | if (loop_dev.has_children()){ 727 | // get first iso9660 partition 728 | var list = Device.get_block_devices_using_lsblk(); 729 | foreach(var dev in list){ 730 | if ((dev.pkname == loop_dev.device.replace("/dev/","")) && (dev.fstype == "iso9660")){ 731 | loop_dev = dev; 732 | break; 733 | } 734 | } 735 | } 736 | 737 | // browse 738 | if (loop_dev != null){ 739 | var mps = Device.get_device_mount_points(loop_dev.device); 740 | if (mps.size > 0){ 741 | var mp = mps[0]; 742 | exo_open_folder(mp.mount_point); 743 | } 744 | } 745 | } 746 | }); 747 | } 748 | 749 | menu.show_all(); 750 | 751 | return menu; 752 | } 753 | 754 | private Device? luks_unlock(Device dev_locked){ 755 | 756 | var dev = dev_locked; 757 | 758 | if (!dev.is_encrypted_partition()){ 759 | log_error("luks_unlock: is_encrypted_partition(): false"); 760 | return null; 761 | } 762 | 763 | var dummy_window = new Gtk.Window(); 764 | 765 | App.init_daemon(); 766 | 767 | while (!App.daemon.is_ready){ 768 | sleep(100); 769 | } 770 | 771 | log_debug("Prompting user for passphrase.."); 772 | 773 | var password = gtk_inputbox( 774 | _("Encrypted Device"), 775 | _("Enter passphrase to unlock '%s'").printf(dev.device), 776 | dummy_window, true); 777 | 778 | if (password == null){ 779 | log_debug("User cancelled the password prompt"); 780 | return null; 781 | } 782 | 783 | string cmd = "luks_unlock|%s|%s".printf(dev.device, password); 784 | App.daemon.send_command(cmd); 785 | 786 | Device dev_unlocked = null; 787 | dev = dev.query_changes(); 788 | if (dev.has_children()){ 789 | dev_unlocked = dev.children[0]; 790 | } 791 | 792 | return dev_unlocked; 793 | } 794 | 795 | private bool luks_lock(Device dev_unlocked){ 796 | 797 | Device dev_luks = null; 798 | if (dev_unlocked.is_on_encrypted_partition()){ 799 | dev_luks = dev_unlocked; 800 | } 801 | else if (dev_unlocked.is_encrypted_partition() && (dev_unlocked.has_children())){ 802 | dev_luks = dev_unlocked.children[0]; 803 | } 804 | 805 | App.init_daemon(); 806 | 807 | while (!App.daemon.is_ready){ 808 | sleep(100); 809 | } 810 | 811 | string cmd = "luks_lock|%s".printf(dev_luks.device); 812 | int status = App.daemon.send_command(cmd); 813 | return (status == 0); 814 | } 815 | 816 | private void open_baobab(Device dev){ 817 | 818 | if ((dev.used_bytes > 0) && (dev.mount_points.size > 0)){ 819 | string cmd = "baobab '%s'\n".printf(dev.mount_points[0].mount_point); 820 | string std_out, std_err; 821 | exec_script_sync(cmd, out std_out, out std_err, false, true); 822 | } 823 | 824 | /*if ((dev.used_bytes > 0) && (dev.mount_points.size > 0)){ 825 | 826 | App.init_daemon(); 827 | 828 | while (!App.daemon.is_ready){ 829 | sleep(100); 830 | } 831 | 832 | string cmd = "open_baobab|%s".printf(dev.mount_points[0].mount_point); 833 | App.daemon.send_command(cmd); 834 | }*/ 835 | } 836 | 837 | public void btn_donate_clicked(){ 838 | var dialog = new DonationWindow(); 839 | //dialog.set_transient_for(this); 840 | dialog.show_all(); 841 | dialog.run(); 842 | dialog.destroy(); 843 | } 844 | 845 | private void btn_about_clicked (){ 846 | 847 | var dialog = new AboutWindow(); 848 | //dialog.set_transient_for(this); 849 | 850 | dialog.authors = { 851 | "Tony George:teejeetech@gmail.com" 852 | }; 853 | 854 | dialog.translators = { 855 | 856 | }; 857 | 858 | dialog.contributors = { 859 | 860 | }; 861 | 862 | dialog.third_party = { 863 | 864 | }; 865 | 866 | dialog.documenters = null; 867 | dialog.artists = null; 868 | dialog.donations = null; 869 | 870 | dialog.program_name = AppName; 871 | dialog.comments = _("Disk Indicator for Linux"); 872 | dialog.copyright = "Copyright © 2016 Tony George (%s)".printf(AppAuthorEmail); 873 | dialog.version = AppVersion; 874 | dialog.logo = get_shared_icon_pixbuf("disks","disks.png", 128); 875 | 876 | dialog.license = "This program is free for personal and commercial use and comes with absolutely no warranty. You use this program entirely at your own risk. The author will not be liable for any damages arising from the use of this program."; 877 | dialog.website = "http://teejeetech.in"; 878 | dialog.website_label = "http://teejeetech.blogspot.in"; 879 | 880 | dialog.initialize(); 881 | dialog.show_all(); 882 | } 883 | 884 | } 885 | 886 | --------------------------------------------------------------------------------