Debian Codegraph @

components/extension/donate/methods/paypal.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2025 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include "paypal.h"
14 #include "../dialog.h"
15
16 using namespace smooth::IO;
17
18 static PaymentMethod *MethodPayPalFactory()
19 {
20 return new MethodPayPal();
21 }
22
23 Bool methodPayPalRegistered = DonateDialog::RegisterPaymentMethod(&MethodPayPalFactory);
24
25 BoCA::MethodPayPal::MethodPayPal() : PaymentMethod("paypal")
26 {
27 layer = NIL;
28
29 /* Load PayPal logo.
30 */
31 Directory dir(Utilities::GetBoCADirectory().Append("freac.extension.donate"));
32
33 if (!dir.Exists()) dir = Utilities::GetBoCADirectory().Append("../freac/freac.extension.donate");
34
35 logo = ImageLoader::Load(String(dir).Append("/paypal/logo.png"));
36 }
37
38 BoCA::MethodPayPal::~MethodPayPal()
39 {
40 if (layer != NIL) Object::DeleteObject(layer);
41 }
42
43 Layer *BoCA::MethodPayPal::GetLayer()
44 {
45 if (layer == NIL) layer = new LayerPayPal();
46
47 return layer;
48 }
49
50 BoCA::LayerPayPal::LayerPayPal()
51 {
52 I18n *i18n = I18n::Get();
53
54 i18n->SetContext("Extensions::Donate::PayPal");
55
56 text_donate = new Text(i18n->TranslateString("Choose an amount and click the button to make a donation with PayPal:"), Point(7, 4));
57 text_donate_direct = new Text(i18n->TranslateString("You can also send money directly to %1 with PayPal.").Replace("%1", "[email protected]"), Point(7, text_donate->GetUnscaledTextHeight() + 73));
58
59 Add(text_donate);
60 Add(text_donate_direct);
61
62 /* Controls to choose amount.
63 */
64 const Array<String> &levels = i18n->TranslateString("10,15,20,other amount").Explode(",");
65
66 monthly = False;
67
68 combo_amount = new ComboBox(Point(100, text_donate->GetUnscaledTextHeight() + 20), Size(100, 0));
69
70 for (Int i = 0; i < 3; i++) combo_amount->AddEntry(i18n->TranslateString("$%1").Replace("%1", levels.GetNth(i)));
71
72 combo_amount->AddEntry(levels.GetNth(3));
73 combo_amount->SelectNthEntry(0);
74
75 combo_amount->onSelectEntry.Connect(&LayerPayPal::OnChangeSettings, this);
76
77 check_monthly = new CheckBox(i18n->TranslateString("monthly recurring"), Point(100, combo_amount->GetY() + 23), Size(0, 0), &monthly);
78 check_monthly->onAction.Connect(&LayerPayPal::OnChangeSettings, this);
79 check_monthly->SetWidth(Math::Max(combo_amount->GetFont().GetUnscaledTextSizeX(levels.GetNth(3)) + 24, check_monthly->GetUnscaledTextWidth() + 23));
80
81 combo_amount->SetWidth(check_monthly->GetWidth());
82
83 Add(combo_amount);
84 Add(check_monthly);
85
86 /* Donate button.
87 */
88 Directory dir(Utilities::GetBoCADirectory().Append("freac.extension.donate"));
89
90 if (!dir.Exists()) dir = Utilities::GetBoCADirectory().Append("../freac/freac.extension.donate");
91
92 Bitmap paypal = ImageLoader::Load(String(dir).Append("/paypal/paypal.png"));
93
94 link_donate = new Hyperlink(paypal, String("file:///").Append(S::System::System::GetTempDirectory()).Append("pp").Append(Number((Int64) this).ToHexString(8)).Append(".html").Replace("\\", "/"), Point(0, text_donate->GetUnscaledTextHeight() + 18), Size(96, 44));
95 text_donate_now = new Text(i18n->TranslateString("Donate"), link_donate->GetPosition() + Point(10, 24));
96
97 Font font = text_donate_now->GetFont();
98
99 font.SetColor(Color(0, 45, 137));
100 font.SetWeight(Font::Bold);
101 font.SetSize(9);
102
103 text_donate_now->SetFont(font);
104 text_donate_now->SetBackgroundColor(Color(255, 255, 255));
105
106 Add(link_donate);
107 Add(text_donate_now);
108
109 OnChangeSettings();
110
111 onChangeSize.Connect(&LayerPayPal::OnChangeSize, this);
112
113 SetSize(Size(Math::Max(text_donate->GetUnscaledTextWidth(), text_donate_direct->GetUnscaledTextWidth()) + 15, text_donate->GetUnscaledTextHeight() + text_donate_direct->GetUnscaledTextHeight() + 79));
114 }
115
116 BoCA::LayerPayPal::~LayerPayPal()
117 {
118 File(S::System::System::GetTempDirectory().Append("pp").Append(Number((Int64) this).ToHexString(8)).Append(".html")).Delete();
119
120 DeleteObject(text_donate);
121 DeleteObject(text_donate_direct);
122
123 DeleteObject(combo_amount);
124 DeleteObject(check_monthly);
125
126 DeleteObject(link_donate);
127 DeleteObject(text_donate_now);
128 }
129
130 Void BoCA::LayerPayPal::OnChangeSize(const Size &nSize)
131 {
132 combo_amount->SetX((nSize.cx - combo_amount->GetWidth() - link_donate->GetWidth() - 8) / 2.0);
133 check_monthly->SetX(combo_amount->GetX());
134
135 link_donate->SetX(combo_amount->GetX() + combo_amount->GetWidth() + 8);
136 text_donate_now->SetX(link_donate->GetX() + Math::Round((link_donate->GetWidth() - text_donate_now->GetWidth()) / 2.0));
137 }
138
139 Void BoCA::LayerPayPal::OnChangeSettings()
140 {
141 I18n *i18n = I18n::Get();
142
143 i18n->SetContext("Extensions::Donate::PayPal");
144
145 const Array<String> &levels = i18n->TranslateString("10,15,20,other amount").Explode(",");
146
147 /* Disable monthly donations for other amounts.
148 */
149 if (combo_amount->GetSelectedEntryNumber() == 3)
150 {
151 check_monthly->SetChecked(False);
152 check_monthly->Deactivate();
153 }
154 else
155 {
156 check_monthly->Activate();
157 }
158
159 /* Update donation URL.
160 */
161 String url = "https://www.paypal.com/cgi-bin/webscr?cmd=%method%&business=%email%&item_name=%reference%&no_note=1&no_shipping=1&amount=%amount%&a3=%amount%&p3=1&t3=M&src=%subscription%&return=%website%&cancel_return=%website%&currency_code=%currency%";
162
163 url = String(url).Replace("%reference%", Encoding::URLEncode::Encode("fre:ac project donation"))
164 .Replace("%email%", Encoding::URLEncode::Encode("[email protected]"))
165 .Replace("%amount%", combo_amount->GetSelectedEntryNumber() < 3 ? levels.GetNth(combo_amount->GetSelectedEntryNumber()) : "0")
166 .Replace("%currency%", i18n->TranslateString("usd").ToUpper())
167 .Replace("%subscription%", monthly ? "1" : "0")
168 .Replace("%method%", monthly ? "_xclick-subscriptions" : "_xclick")
169 .Replace("%website%", Encoding::URLEncode::Encode("https://www.freac.org/"));
170
171 link_donate->SetURL(url);
172 }