forked from mijorus/gearlever
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
158 lines (127 loc) · 5.35 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# main.py
#
# Copyright 2022 Lorenzo Paderi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import gi
import logging
import os
from .lib.utils import log, get_gsettings
from .lib.costants import APP_ID, APP_NAME
from .providers.providers_list import appimage_provider
from .GearleverWindow import GearleverWindow
from .WelcomeScreen import WelcomeScreen
from .preferences import Preferences
gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')
from gi.repository import Gtk, Gio, Adw, Gdk, GLib, GObject # noqa
LOG_FILE_MAX_N_LINES = 5000
LOG_FOLDER = GLib.get_user_cache_dir() + '/logs'
class GearleverApplication(Adw.Application):
"""The main application singleton class."""
def __init__(self, version, pkgdatadir):
super().__init__(application_id=APP_ID, flags=Gio.ApplicationFlags.HANDLES_OPEN)
self.create_action('about', self.on_about_action)
self.create_action('preferences', self.on_preferences_action)
self.create_action('open_log_file', self.on_open_log_file)
self.create_action('open_welcome_screen', self.on_open_welcome_screen)
self.win = None
self.version = version
self.pkgdatadir = pkgdatadir
def do_startup(self):
log('\n\n---- Application startup')
Adw.Application.do_startup(self)
css_provider = Gtk.CssProvider()
css_provider.load_from_resource(f'/it/mijorus/{APP_NAME}/assets/style.css')
Gtk.StyleContext.add_provider_for_display(Gdk.Display.get_default(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
def do_activate(self, from_file=False):
"""Called when the application is activated.
We raise the application's main window, creating it if
necessary.
"""
self.win = self.props.active_window
if not self.win:
self.win = GearleverWindow(application=self, from_file=from_file)
# if True:
if not get_gsettings().get_boolean('first-run'):
get_gsettings().set_boolean('first-run', True)
tutorial = WelcomeScreen(self.pkgdatadir)
tutorial.present()
self.win.present()
def do_open(self, files: list[Gio.File], n_files: int, data):
if files and appimage_provider.can_install_file(files[0]):
self.do_activate(from_file=True)
self.win.on_selected_local_file(files[0])
def on_about_action(self, widget, data):
about = Adw.AboutWindow(
application_name='Gear lever',
version=self.version,
developers=['Lorenzo Paderi'],
copyright='2023 Lorenzo Paderi',
application_icon='it.mijorus.gearlever',
issue_url='https://github.com/mijorus/gearlever',
)
about.set_translator_credits(_("translator_credits"))
about.present()
def on_preferences_action(self, widget, _):
"""Callback for the app.preferences action."""
pref = Preferences()
pref.connect('close-request', lambda w: self.win.on_show_installed_list() if self.win else None)
pref.present()
def create_action(self, name, callback, shortcuts=None):
"""Add an application action.
Args:
name: the name of the action
callback: the function to be called when the action is
activated
shortcuts: an optional list of accelerators
"""
action = Gio.SimpleAction.new(name, None)
action.connect("activate", callback)
self.add_action(action)
if shortcuts:
self.set_accels_for_action(f"app.{name}", shortcuts)
def on_open_log_file(self, widget, event):
if not self.win:
return
log_gfile = Gio.File.new_for_path(f'{GLib.get_user_cache_dir()}/logs')
launcher = Gtk.FileLauncher.new(log_gfile)
launcher.launch()
def on_open_welcome_screen(self, widget, event):
tutorial = WelcomeScreen(self.pkgdatadir)
tutorial.present()
def main(version, pkgdatadir):
"""The application's entry point."""
log_file = f'{LOG_FOLDER}/{APP_NAME}.log'
if not os.path.exists(LOG_FOLDER):
os.makedirs(LOG_FOLDER)
print('Logging to file ' + log_file)
# Clear log file if it's too big
log_file_size = 0
if os.path.exists(log_file):
with open(log_file, 'r') as f:
log_file_size = len(f.readlines())
if log_file_size > LOG_FILE_MAX_N_LINES:
with open(log_file, 'w+') as f:
f.write('')
app = GearleverApplication(version, pkgdatadir)
logging.basicConfig(
filename=log_file,
filemode='a',
encoding='utf-8',
level= logging.DEBUG if get_gsettings().get_boolean('debug-logs') else logging.INFO,
force=True
)
return app.run(sys.argv)