main.py 3.22 KB
Newer Older
Roman Alifanov's avatar
Roman Alifanov committed
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
# main.py
#
# Copyright 2024 Etersoft
#
# 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 <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later

import sys
import gi

gi.require_version('Gtk', '4.0')
gi.require_version('Adw', '1')

from gi.repository import Gtk, Gio, Adw
from .window import TuneitWindow

29 30
def get_main_window():
    return _application.props.active_window
Roman Alifanov's avatar
Roman Alifanov committed
31

Roman Alifanov's avatar
Roman Alifanov committed
32 33 34
def get_error():
    return _application.props.active_window.error

Roman Alifanov's avatar
Roman Alifanov committed
35 36 37 38
class TuneitApplication(Adw.Application):
    """The main application singleton class."""

    def __init__(self):
39
        super().__init__(application_id='ru.ximperlinux.TuneIt',
Roman Alifanov's avatar
Roman Alifanov committed
40
                         flags=Gio.ApplicationFlags.DEFAULT_FLAGS)
41
        global _application
Roman Alifanov's avatar
Roman Alifanov committed
42 43 44 45
        self.create_action('quit', lambda *_: self.quit(), ['<primary>q'])
        self.create_action('about', self.on_about_action)
        self.create_action('preferences', self.on_preferences_action)

46 47
        _application = self

Roman Alifanov's avatar
Roman Alifanov committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61
    def do_activate(self):
        """Called when the application is activated.

        We raise the application's main window, creating it if
        necessary.
        """
        win = self.props.active_window
        if not win:
            win = TuneitWindow(application=self)
        win.present()

    def on_about_action(self, *args):
        """Callback for the app.about action."""
        about = Adw.AboutDialog(application_name='tuneit',
62
                                application_icon='ru.ximperlinux.TuneIt',
Roman Alifanov's avatar
Roman Alifanov committed
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
                                developer_name='Etersoft',
                                version='0.1.0',
                                developers=['Ximper'],
                                copyright='© 2024 Etersoft')
        # Translators: Replace "translator-credits" with your name/username, and optionally an email or URL.
        about.set_translator_credits(_('translator-credits'))
        about.present(self.props.active_window)

    def on_preferences_action(self, widget, _):
        """Callback for the app.preferences action."""
        print('app.preferences action activated')

    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 main(version):
    """The application's entry point."""
    app = TuneitApplication()
    return app.run(sys.argv)