-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
158 lines (136 loc) · 6.37 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
import subprocess
import sys
import os
import webbrowser
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QMainWindow, QMessageBox
from PyQt5.QtCore import Qt
from ui_mainwindow import Ui_MainWindow
from ui_initialize import Ui_MainWindow as Ui_Initialize
class MainWindow(QMainWindow):
# Class definition for the main application window
def check_file_exists(self):
# Checks if a file exists
appdata_dir = os.getenv('APPDATA') # Get the path to the AppData directory
saves_dir = os.path.join(appdata_dir, 'ThemeCord', 'Saves') # Create the path to the saves directory
file_path = os.path.join(saves_dir, 'theme-cord.installed') # Create the path to the file
if os.path.exists(file_path):
print("File exists!")
return True
else:
print("File does not exist!")
return False
def __init__(self):
# Constructor for the MainWindow class
super().__init__()
if self.check_file_exists():
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.setFixedSize(861, 535)
self.setWindowFlags(self.windowFlags() & ~Qt.WindowMaximizeButtonHint)
self.setWindowTitle("Theme Cord")
self.ui.Discord.clicked.connect(self.open_discord)
self.ui.Github.clicked.connect(self.open_github)
self.ui.pushButton.clicked.connect(self.apply)
self.ui.pushButton_2.clicked.connect(self.remove_theme)
else:
self.ui = Ui_Initialize()
self.ui.setupUi(self)
self.setFixedSize(546,743)
self.setWindowFlags(self.windowFlags() & ~Qt.WindowMaximizeButtonHint)
self.setWindowTitle("Initialize")
self.ui.Discord.clicked.connect(self.open_discord)
self.ui.Github.clicked.connect(self.open_github)
self.ui.Website.clicked.connect(self.open_website)
self.ui.WhyInitialize.clicked.connect(self.open_in)
self.ui.Initialize.clicked.connect(self.init)
def apply(self):
# Applies a new theme to Discord
# Get the input from the QTextEdit
theme_text = self.ui.themeText.toPlainText()
# Check if the input is empty or equals "Enter your theme link"
if theme_text == "Enter your theme link" or theme_text == "" or theme_text == "Please fill me!":
return self.ui.themeText.setText("Please fill me!")
# Set the path to the CSS file
css_path = os.path.join(os.path.dirname(__file__), "themecord", "template.css")
# Create the new CSS content
file_content = f"@import url('{theme_text}');"
# Append the new CSS content to the file
with open(css_path, 'a') as file:
file.write(file_content)
message_box = QMessageBox()
message_box.setWindowTitle("New theme added!")
message_box.setText("ThemeCord has successfully added a new theme to Discord!")
message_box.setIcon(QMessageBox.Information)
message_box.addButton(QMessageBox.Ok)
message_box.exec_()
def remove_theme(self):
# Removes the applied theme from Discord
# Set the path to the CSS file
css_path = os.path.join(os.path.dirname(__file__), "themecord", "template.css")
# Define the CSS content
file_content = """
/* CSS style for ThemeCord */
/* Reset! */
"""
# Write the CSS file
with open(css_path, 'w') as file:
file.write(file_content)
# Show a success message box
message_box = QMessageBox()
message_box.setWindowTitle("All the themes are now removed!")
message_box.setText("ThemeCord has been successfully removed every theme you applied to the discord.")
message_box.setIcon(QMessageBox.Information)
message_box.addButton(QMessageBox.Ok)
message_box.exec_()
return
def init(self):
# Initializes the application and injects the theme into Discord
# Set the path to the CSS file
css_path = os.path.join(os.path.dirname(__file__), "themecord", "template.css")
# Define the CSS content
file_content = ""
# Write the CSS file
with open(css_path, 'w') as file:
file.write(file_content)
# Create the directory if it doesn't exist
appdata_dir = os.getenv('APPDATA') # Get the path to the AppData directory
saves_dir = os.path.join(appdata_dir, 'ThemeCord', 'Saves') # Create the path to the saves directory
os.makedirs(saves_dir, exist_ok=True) # Create the directory if it doesn't exist
# Write the "theme-cord.installed" file
file_path = os.path.join(saves_dir, 'theme-cord.installed') # Create the path to the file
with open(file_path, 'w') as file:
file.write("true")
# Run the ThemeCord injector
subprocess.run([os.path.join(os.path.dirname(__file__), "themecord", "injector.exe"), "--css", css_path], check=True)
# Show a success message box
message_box = QMessageBox()
message_box.setWindowTitle("Initialization Successful")
message_box.setText("ThemeCord has been successfully initialized in the current Discord session. Please close and re-launch the app to start using the app.")
message_box.setIcon(QMessageBox.Information)
message_box.addButton(QMessageBox.Ok)
message_box.exec_()
def open_url(self, url):
# Opens a given URL in a web browser
if webbrowser.open(url):
print("URL opened successfully.")
else:
print("Failed to open the URL.")
def open_discord(self):
# Opens the Discord URL in a web browser
return self.open_url("https://discord.gg/FTK3txBsAS")
def open_github(self):
# Opens the Github URL in a web browser
return self.open_url("https://github.com/Theme-Cord")
def open_website(self):
# Opens the Website URL in a web browser
return self.open_url("https://Theme-Cord.Github.Io")
def open_in(self):
# Opens a specific URL in a web browser
return self.open_url("https://github.com/Theme-Cord/.github/blob/main/why_init.md")
if __name__ == "__main__":
# Entry point of the script
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())