Skip to content

Commit 01fc6bd

Browse files
mirkobrombinTheEvilSkeleton
authored andcommitted
donation: Add Donation dialog
1 parent 5020d62 commit 01fc6bd

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

bottles/backend/managers/data.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
class UserDataKeys:
3030
CustomBottlesPath = "custom_bottles_path"
31+
FundingDismissed = "funding_dialog_dismissed"
3132

3233

3334
class DataManager:

bottles/backend/managers/journal.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ def get_event(event_id: str):
171171
journal = JournalManager.__get_journal()
172172
return journal.get(event_id, None)
173173

174+
@staticmethod
175+
def first_event_date():
176+
"""Return the timestamp of the oldest event as datetime."""
177+
journal = JournalManager.__get_journal()
178+
first = None
179+
for event in journal.values():
180+
timestamp = event.get("timestamp")
181+
if not timestamp:
182+
continue
183+
try:
184+
ts = datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S")
185+
except (ValueError, TypeError):
186+
continue
187+
if first is None or ts < first:
188+
first = ts
189+
return first
190+
174191
@staticmethod
175192
def write(severity: JournalSeverity, message: str):
176193
"""Write an event to the journal."""

bottles/backend/models/samples.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Samples:
2-
data = {}
2+
data = {
3+
"funding_dialog_dismissed": False,
4+
}
35
environments = {
46
"gaming": {
57
"Runner": "wine",

bottles/frontend/window.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
from bottles.backend.globals import Paths
2626
from bottles.backend.health import HealthChecker
2727
from bottles.backend.logger import Logger
28-
from bottles.backend.managers.data import UserDataKeys
28+
from datetime import datetime
29+
30+
from bottles.backend.managers.data import DataManager, UserDataKeys
31+
from bottles.backend.managers.journal import JournalManager
2932
from bottles.backend.managers.manager import Manager
3033
from bottles.backend.models.config import BottleConfig
3134
from bottles.backend.models.result import Result
@@ -78,6 +81,16 @@ def __init__(self, arg_bottle, **kwargs):
7881

7982
super().__init__(**kwargs, default_width=width, default_height=height)
8083

84+
self.data_mgr = DataManager()
85+
first_event = JournalManager.first_event_date()
86+
days_old = 0
87+
if first_event:
88+
days_old = (datetime.now() - first_event).days
89+
90+
self._show_funding = days_old >= 7 and not self.data_mgr.get(
91+
UserDataKeys.FundingDismissed
92+
)
93+
8194
self.utils_conn = ConnectionUtils(
8295
force_offline=self.settings.get_boolean("force-offline")
8396
)
@@ -163,6 +176,7 @@ def response(dialog, response, *args):
163176
logging.info(
164177
"Bottles Started!",
165178
)
179+
GLib.idle_add(self.__maybe_show_funding_dialog)
166180

167181
@Gtk.Template.Callback()
168182
def on_close_request(self, *args):
@@ -358,6 +372,30 @@ def check_crash_log(self):
358372
if crash_log:
359373
CrashReportDialog(self, crash_log).present()
360374

375+
def __maybe_show_funding_dialog(self):
376+
if not self._show_funding:
377+
return
378+
379+
dialog = Adw.MessageDialog.new(
380+
self,
381+
_("Support Bottles"),
382+
_(
383+
"With over 3 million installations, Bottles is built by and for its community."
384+
"\nA donation today helps secure its future and keep it truly independent."
385+
),
386+
)
387+
dialog.add_response("donate", _("Donate"))
388+
dialog.add_response("dismiss", _("Don't Show Again"))
389+
dialog.set_response_appearance("donate", Adw.ResponseAppearance.SUGGESTED)
390+
dialog.connect("response", self.__funding_response)
391+
dialog.present()
392+
393+
def __funding_response(self, dialog, response):
394+
if response == "donate":
395+
self.open_url(None, "https://usebottles.com/funding/")
396+
self.data_mgr.set(UserDataKeys.FundingDismissed, True)
397+
dialog.destroy()
398+
361399
def toggle_selection_mode(self, status: bool = True):
362400
context = self.headerbar.get_style_context()
363401
if status:

0 commit comments

Comments
 (0)