-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.py
More file actions
1252 lines (1091 loc) · 47.8 KB
/
Main.py
File metadata and controls
1252 lines (1091 loc) · 47.8 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import os
import json
import warnings
import ctypes # Add this import
import subprocess # Add this for subprocess.Popen
import importlib.util # Add this for dynamic imports
from PyQt6.QtWidgets import QMessageBox, QStatusBar, QProgressBar, QApplication # Add these for UI elements
from PyQt6.QtCore import QTimer # Add this for timer functionality
import traceback # Add this for detailed error tracking
import threading # Add this for threading support
import signal # Add this for signal handling
import atexit # Add this for exit handling
def handle_exception(exc_type, exc_value, exc_traceback):
"""Global exception handler to catch unhandled exceptions"""
# Don't handle KeyboardInterrupt
if issubclass(exc_type, KeyboardInterrupt):
sys.__excepthook__(exc_type, exc_value, exc_traceback)
return
# Format the error message
error_msg = ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback))
# Log the error
print("Unhandled exception occurred:")
print(error_msg)
# Show error dialog to user
try:
QMessageBox.critical(
None,
"Application Error",
f"An unexpected error occurred:\n\n{error_msg}\n\n"
"The application will now close. Please restart it."
)
except:
# If we can't show a dialog, just print to console
print("Failed to show error dialog")
# Clean up and exit
sys.exit(1)
# Set the global exception handler
sys.excepthook = handle_exception
# Suppress Wireshark warning
warnings.filterwarnings("ignore", category=UserWarning)
# Get the absolute path of the current directory
current_dir = os.path.dirname(os.path.abspath(__file__))
# Debug: Print current directory and list files
print(f"Current directory: {current_dir}")
print("\nFiles in directory:")
for file in os.listdir(current_dir):
print(f"- {file}")
# Import using full path
real_time_packet_filtering_path = os.path.join(current_dir, "Real_time_Packet_Filtering.py")
print(f"\nLooking for file at: {real_time_packet_filtering_path}")
if os.path.exists(real_time_packet_filtering_path):
print("File found, attempting to import...")
spec = importlib.util.spec_from_file_location("Real_time_Packet_Filtering", real_time_packet_filtering_path)
Real_time_Packet_Filtering = importlib.util.module_from_spec(spec)
spec.loader.exec_module(Real_time_Packet_Filtering)
PacketAnalyzer = Real_time_Packet_Filtering.PacketAnalyzer
print("Import successful!")
else:
print(f"Could not find Real_time_Packet_Filtering.py at {real_time_packet_filtering_path}")
sys.exit(1)
from PyQt6.QtWidgets import (QApplication, QMainWindow, QPushButton,
QVBoxLayout, QHBoxLayout, QWidget, QLabel,
QSizePolicy)
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QFont, QColor
# Import local modules with dependency checking
def check_report_dependencies():
try:
# Check if SimpleReport.py exists
simple_report_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'SimpleReport.py')
if os.path.exists(simple_report_path):
print(f"SimpleReport.py found at {simple_report_path}")
return True
else:
print(f"SimpleReport.py not found at {simple_report_path}")
return False
except Exception as e:
print(f"Error checking dependencies: {str(e)}")
return False
try:
from Monitoring_Dashboard import NetworkMonitorApp
from Basic_Network_Troubleshoot import NetworkTroubleshooter
from Network_Device_And_Vulnerability_Scan import MainWindow as NetworkDeviceScan
from WIFI_Signals_Details import WifiScannerApp
# Only import SimpleReport if dependencies are available
if check_report_dependencies():
from SimpleReport import SimpleReportWindow
REPORT_AVAILABLE = True
else:
REPORT_AVAILABLE = False
print("Warning: Report functionality not available. Please install reportlab and Pillow.")
except ImportError as e:
print(f"Error importing modules: {e}")
print(f"Current directory: {current_dir}")
print(f"Python path: {sys.path}")
sys.exit(1)
class MonitoringDashboard(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Network Monitoring Tool")
self.setStyleSheet("background-color: black;")
# Set cleanup flags for windows
self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
# Fix window size
self.setFixedSize(900, 650)
# Initialize windows to None
self.monitoring_window = None
self.network_troubleshoot_window = None
self.network_device_scan_window = None
self.wifi_signals_window = None
self.packet_analyzer_window = None
self.report_window = None if REPORT_AVAILABLE else None
# Initialize app.py process reference
self.app_py_process = None
# Create status bar
self.statusBar = QStatusBar()
self.statusBar.setStyleSheet("background-color: #333; color: white;")
self.setStatusBar(self.statusBar)
# Create SMS Alert toggle button for status bar
self.sms_alert_btn = QPushButton("SMS Alert: ON")
self.sms_alert_btn.setFont(QFont("Arial", 9))
self.sms_alert_btn.clicked.connect(self.toggle_sms_alert)
self.sms_alert_btn.setStyleSheet("""
QPushButton {
background-color: #4CAF50;
color: white;
border-radius: 3px;
padding: 3px 8px;
text-align: center;
min-height: 20px;
max-width: 100px;
}
QPushButton:hover {
background-color: #45a049;
}
QPushButton:pressed {
background-color: #3c8c40;
}
""")
self.sms_alert_btn.setFixedSize(100, 20)
# Load current SMS alert state
self.load_sms_alert_state()
# Add SMS Alert button to status bar (left side)
self.statusBar.addWidget(self.sms_alert_btn)
# Create progress bar
self.progressBar = QProgressBar()
self.progressBar.setRange(0, 100)
self.progressBar.setTextVisible(True)
self.progressBar.setStyleSheet("""
QProgressBar {
border: 1px solid #76797C;
border-radius: 5px;
text-align: center;
color: white;
background-color: #333;
}
QProgressBar::chunk {
background-color: #05B8CC;
border-radius: 5px;
}
""")
self.progressBar.setFixedWidth(200)
self.progressBar.hide() # Hide initially
# Add progress bar to status bar (right side)
self.statusBar.addPermanentWidget(self.progressBar)
# Initialize window states
self.window_states = {
'packet_analyzer': False,
'network_device_scan': False,
'wifi_signals': False,
'network_troubleshoot': False
}
# Connect window close events
self.connect_window_close_events()
# Create main layout
main_layout = QVBoxLayout()
main_layout.setSpacing(20)
main_layout.setContentsMargins(30, 30, 30, 30)
# Title
title_label = QLabel("Main Window")
title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
title_font = QFont("Arial", 48, QFont.Weight.Bold)
title_label.setFont(title_font)
title_label.setStyleSheet("color: #ff6347;")
main_layout.addWidget(title_label)
# Monitoring Dashboard button
dashboard_button = QPushButton("Monitoring Dashboard")
dashboard_font = QFont("Arial", 32, QFont.Weight.Bold)
dashboard_button.setFont(dashboard_font)
dashboard_button.setStyleSheet("""
QPushButton {
background-color: #7ED957;
color: black;
border-radius: 25px;
padding: 15px;
margin: 10px 0px;
text-align: center;
}
QPushButton:hover {
background-color: #8FE968;
}
QPushButton:pressed {
background-color: #6DC946;
}
""")
dashboard_button.clicked.connect(self.open_monitoring_dashboard)
main_layout.addWidget(dashboard_button)
# First row of buttons
first_row = QHBoxLayout()
first_row.setSpacing(20)
# Basic Networking Troubleshoot button
basic_network_btn = self.create_button("Basic Networking\nTroubleshoot")
basic_network_btn.clicked.connect(self.open_network_troubleshoot)
first_row.addWidget(basic_network_btn)
# Network Device And Vulnerability Scan button
network_device_btn = self.create_button("Network Device And\nVulnerability Scan")
network_device_btn.clicked.connect(self.open_network_device_scan)
first_row.addWidget(network_device_btn)
main_layout.addLayout(first_row)
# Second row of buttons
second_row = QHBoxLayout()
second_row.setSpacing(20)
# Real Time Packet Filtering button
real_time_btn = self.create_button("Real Time Packet\nFiltering")
real_time_btn.clicked.connect(self.open_packet_analyzer)
second_row.addWidget(real_time_btn)
# WiFi Signals Details button
wifi_signals_btn = self.create_button("WiFi Signals Details")
wifi_signals_btn.clicked.connect(self.open_wifi_signals)
second_row.addWidget(wifi_signals_btn)
main_layout.addLayout(second_row)
# Third row of buttons
third_row = QHBoxLayout()
third_row.setSpacing(20)
# Alert System button
alert_system_btn = self.create_button("Alert System")
alert_system_btn.clicked.connect(self.open_alert_system)
third_row.addWidget(alert_system_btn)
# Generate Report button
generate_report_btn = self.create_button("Generate Report")
generate_report_btn.clicked.connect(self.open_detail_report)
if not REPORT_AVAILABLE:
generate_report_btn.setEnabled(False)
generate_report_btn.setToolTip(
"Report functionality requires additional packages.\n"
"Please install: reportlab and pillow"
)
third_row.addWidget(generate_report_btn)
main_layout.addLayout(third_row)
# Set central widget
central_widget = QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
def open_monitoring_dashboard(self):
"""Open the Monitoring Dashboard window"""
try:
print("Opening Monitoring Dashboard...")
# Check if window exists but is hidden
if self.monitoring_window and not self.monitoring_window.isVisible():
self.monitoring_window.show()
self.monitoring_window.raise_()
return
# If window doesn't exist or was closed
if self.monitoring_window is None:
try:
self.monitoring_window = NetworkMonitorApp()
self.monitoring_window.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
self.monitoring_window.show()
self.monitoring_window.raise_()
except Exception as e:
print(f"Error creating Monitoring Dashboard: {str(e)}")
QMessageBox.critical(
self,
"Error",
f"Failed to create Monitoring Dashboard:\n{str(e)}\n\n"
"Please try again or restart the application."
)
self.monitoring_window = None
return
except Exception as e:
print(f"Error opening Monitoring Dashboard: {str(e)}")
QMessageBox.critical(
self,
"Error",
f"Failed to open Monitoring Dashboard:\n{str(e)}\n\n"
"Please try again or restart the application."
)
# Clean up any partially created window
if hasattr(self, 'monitoring_window') and self.monitoring_window:
self.monitoring_window.close()
self.monitoring_window.deleteLater()
self.monitoring_window = None
def open_network_troubleshoot(self):
"""Open the Network Troubleshoot window"""
try:
print("Opening Network Troubleshoot...") # Debug print
if self.network_troubleshoot_window is None:
self.network_troubleshoot_window = NetworkTroubleshooter()
self.network_troubleshoot_window.show()
print("Network Troubleshoot window created and shown") # Debug print
except Exception as e:
print(f"Error opening Network Troubleshoot: {str(e)}") # Debug print
def open_network_device_scan(self):
"""Open the Network Device And Vulnerability Scan window"""
try:
print("Opening Network Device Scanner...") # Debug print
if self.network_device_scan_window is None:
self.network_device_scan_window = NetworkDeviceScan()
self.network_device_scan_window.show()
print("Network Device Scanner window created and shown") # Debug print
except Exception as e:
print(f"Error opening Network Device Scanner: {str(e)}") # Debug print
def open_wifi_signals(self):
"""Open the WiFi Signals Details window"""
try:
print("Opening WiFi Signals Details...") # Debug print
if self.wifi_signals_window is None:
self.wifi_signals_window = WifiScannerApp()
self.wifi_signals_window.show()
print("WiFi Signals Details window created and shown") # Debug print
except Exception as e:
print(f"Error opening WiFi Signals Details: {str(e)}") # Debug print
def open_packet_analyzer(self):
"""Open the Real Time Packet Filtering window"""
try:
print("Opening Real Time Packet Filtering...")
# Check if running with admin privileges
if not self.is_admin():
# Re-run the specific script with admin rights
import ctypes, sys, os
script_path = os.path.join(os.path.dirname(os.path.abspath(__file__)),
'Real_time_Packet_Filtering.py')
ctypes.windll.shell32.ShellExecuteW(
None,
"runas",
sys.executable,
script_path,
None,
1
)
return
# If already running as admin, create window normally
if self.packet_analyzer_window is None:
from Real_time_Packet_Filtering import PacketAnalyzer
self.packet_analyzer_window = PacketAnalyzer()
self.packet_analyzer_window.show()
self.packet_analyzer_window.raise_()
except Exception as e:
print(f"Error opening Real Time Packet Filtering: {str(e)}")
QMessageBox.critical(
self,
"Error",
f"Failed to open Real Time Packet Filtering: {str(e)}\nTry running as Administrator"
)
def open_detail_report(self):
"""Generate a comprehensive report from all available module data"""
try:
print("Opening Detail Report...") # Debug print
# Check if any modules have been run
data_dir = 'data_temp'
if not os.path.exists(data_dir):
os.makedirs(data_dir)
# Check for data files
data_files = [
'real_time_packet.json',
'wifi_signals_details.json',
'network_device_and_vulnerability_scan.json',
'monitoring_dashboard.json',
'network_troubleshoot_history.json'
]
available_data = []
missing_data = []
for file in data_files:
file_path = os.path.join(data_dir, file)
alt_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), file)
if os.path.exists(file_path) or os.path.exists(alt_path):
module_name = file.replace('.json', '').replace('_', ' ').title()
available_data.append(module_name)
else:
module_name = file.replace('.json', '').replace('_', ' ').title()
missing_data.append(module_name)
if not available_data:
QMessageBox.warning(
self,
"No Data Available",
"Please run at least one analysis module first.\n\n"
"No data files found in the data_temp directory."
)
return
# Generate PDF report automatically without any prompts
try:
from Report import SaveInterface
report_generator = SaveInterface()
# Show progress in the status bar
self.statusBar.showMessage("Generating PDF report from existing data...")
self.progressBar.setValue(0)
self.progressBar.show()
QApplication.processEvents() # Force UI update
# Define progress callback function
def update_progress(value):
self.progressBar.setValue(value)
QApplication.processEvents() # Force UI update
# Generate the report silently with progress updates
result = report_generator.generateSilentReport(external_progress_callback=update_progress)
# Hide progress bar after a short delay
QTimer.singleShot(2000, self.progressBar.hide)
QTimer.singleShot(2000, lambda: self.statusBar.clearMessage())
if result:
QMessageBox.information(
self,
"Report Generated",
"PDF report has been generated successfully and saved to the root folder."
)
else:
QMessageBox.warning(
self,
"Report Generation Failed",
"Failed to generate the PDF report. Please check the console for errors."
)
except Exception as e:
print(f"Error generating silent report: {str(e)}")
QMessageBox.critical(
self,
"Error",
f"Failed to generate silent report: {str(e)}\n\n"
"Please make sure Report.py exists and all required packages are installed."
)
except Exception as e:
print(f"Error opening Detail Report: {str(e)}") # Debug print
QMessageBox.critical(
self,
"Error",
f"Failed to open Detail Report:\n{str(e)}\n\n"
"Please make sure Report.py exists and all required packages are installed."
)
def is_admin(self):
"""Check if running with admin privileges"""
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
def create_button(self, text):
"""Helper function to create styled buttons"""
button = QPushButton(text)
button_font = QFont("Arial", 18, QFont.Weight.Bold)
button.setFont(button_font)
# Default style for all buttons
base_style = """
QPushButton {
background-color: #FFA500;
color: black;
border-radius: 20px;
padding: 15px;
text-align: center;
min-height: 80px;
}
QPushButton:hover {
background-color: #FFB52E;
}
QPushButton:pressed {
background-color: #E69500;
}
"""
# Special padding for Monitoring Dashboard button
if text == "Monitoring Dashboard":
button.setStyleSheet(base_style.replace("padding: 15px;", "padding: 35px;"))
else:
button.setStyleSheet(base_style)
button.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
return button
# Removed connect_report_signals method as we're now using file-based data loading
# Removed generate_module_report method as we're now using a centralized report approach
def install_report_dependencies(self):
"""Install required packages for reporting"""
try:
# Check if SimpleReport.py exists
simple_report_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'SimpleReport.py')
if os.path.exists(simple_report_path):
print(f"SimpleReport.py found at {simple_report_path}")
return True
# If SimpleReport.py doesn't exist, create it
msg = QMessageBox()
msg.setIcon(QMessageBox.Icon.Question)
msg.setText("SimpleReport.py is missing. Would you like to create it?")
msg.setWindowTitle("Create SimpleReport")
msg.setStandardButtons(
QMessageBox.StandardButton.Yes |
QMessageBox.StandardButton.No
)
if msg.exec() == QMessageBox.StandardButton.Yes:
print("Creating SimpleReport.py...")
# Create SimpleReport.py
with open(simple_report_path, 'w') as f:
f.write('''
import sys
import socket
import psutil
import datetime
import json
import os
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QPushButton, QLabel, QTextEdit, QFileDialog,
QMessageBox, QProgressDialog)
from PyQt6.QtCore import Qt, QTimer, pyqtSignal, QThread
class SimpleReportWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Simple Report Generator")
self.setGeometry(100, 100, 800, 600)
# Create central widget and layout
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QVBoxLayout(central_widget)
# Create text area
self.text_area = QTextEdit()
self.text_area.setReadOnly(True)
main_layout.addWidget(self.text_area)
# Create buttons
button_layout = QHBoxLayout()
self.save_btn = QPushButton("Save as PDF")
self.save_btn.clicked.connect(self.save_as_pdf)
button_layout.addWidget(self.save_btn)
self.close_btn = QPushButton("Close")
self.close_btn.clicked.connect(self.close)
button_layout.addWidget(self.close_btn)
main_layout.addLayout(button_layout)
# Load data
self.load_data()
def load_data(self):
try:
# Check for data files
data_dir = 'data_temp'
if not os.path.exists(data_dir):
os.makedirs(data_dir)
# Check for data files
data_files = [
'real_time_packet.json',
'wifi_signals_details.json',
'network_device_and_vulnerability_scan.json',
'monitoring_dashboard.json',
'network_troubleshoot_history.json'
]
available_data = []
for file in data_files:
file_path = os.path.join(data_dir, file)
alt_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), file)
if os.path.exists(file_path) or os.path.exists(alt_path):
module_name = file.replace('.json', '').replace('_', ' ').title()
available_data.append(module_name)
# Display available data
report_text = "Simple Report Generator\n"
report_text += "======================\n\n"
report_text += f"Generated on: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
report_text += "Available Data Sources:\n"
report_text += "----------------------\n"
for data_source in available_data:
report_text += f"- {data_source}\n"
self.text_area.setText(report_text)
except Exception as e:
QMessageBox.critical(self, "Error", f"Failed to load data: {str(e)}")
def save_as_pdf(self):
QMessageBox.information(self, "Info", "PDF generation not implemented in this simple version.")
def main():
app = QApplication(sys.argv)
window = SimpleReportWindow()
window.show()
sys.exit(app.exec())
if __name__ == "__main__":
main()
''')
QMessageBox.information(
self,
"Success",
"SimpleReport.py created successfully.\n"
"The application will now restart."
)
# Restart the application
python = sys.executable
os.execl(python, python, *sys.argv)
return True
return False
except Exception as e:
error_msg = f"Failed to create SimpleReport.py: {str(e)}"
print(error_msg)
QMessageBox.critical(
self,
"Error",
f"{error_msg}\n\n"
"Please create SimpleReport.py manually."
)
return False
def check_running_applications(self):
"""Check which applications are running and available for reporting"""
running_apps = []
missing_apps = []
# Reset window states based on actual window status
if hasattr(self, 'packet_analyzer_window') and self.packet_analyzer_window and not self.packet_analyzer_window.isHidden():
running_apps.append("Packet Analysis")
self.window_states['packet_analyzer'] = True
else:
missing_apps.append("Packet Analysis")
self.window_states['packet_analyzer'] = False
if hasattr(self, 'network_device_scan_window') and self.network_device_scan_window and not self.network_device_scan_window.isHidden():
running_apps.append("Network Device Scan")
self.window_states['network_device_scan'] = True
else:
missing_apps.append("Network Device Scan")
self.window_states['network_device_scan'] = False
if hasattr(self, 'wifi_signals_window') and self.wifi_signals_window and not self.wifi_signals_window.isHidden():
running_apps.append("WiFi Signals")
self.window_states['wifi_signals'] = True
else:
missing_apps.append("WiFi Signals")
self.window_states['wifi_signals'] = False
if hasattr(self, 'network_troubleshoot_window') and self.network_troubleshoot_window and not self.network_troubleshoot_window.isHidden():
running_apps.append("Network Troubleshoot")
self.window_states['network_troubleshoot'] = True
else:
missing_apps.append("Network Troubleshoot")
self.window_states['network_troubleshoot'] = False
return running_apps, missing_apps
def connect_window_close_events(self):
"""Connect close events for all windows to track their states"""
windows = {
'packet_analyzer_window': 'packet_analyzer',
'network_device_scan_window': 'network_device_scan',
'wifi_signals_window': 'wifi_signals',
'network_troubleshoot_window': 'network_troubleshoot'
}
for window_attr, state_key in windows.items():
if hasattr(self, window_attr):
window = getattr(self, window_attr)
if window:
window.closeEvent = lambda event, key=state_key: self.handle_window_close(event, key)
def handle_window_close(self, event, window_key):
"""Handle window close events and update states"""
self.window_states[window_key] = False
event.accept()
def destroy_window(self, window_name):
"""Safely destroy a window instance"""
try:
window = getattr(self, window_name, None)
if window:
window.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose) # Ensure proper cleanup
window.close()
window.deleteLater() # Schedule for deletion
setattr(self, window_name, None)
except Exception as e:
print(f"Error destroying {window_name}: {str(e)}")
def clean_temp_files(self):
"""Clean temporary data files"""
try:
# List of files to clean
files_to_clean = [
'real_time_packet.json',
'wifi_signals_details.json',
'network_device_and_vulnerability_scan.json',
'monitoring_dashboard.json'
# Note: We don't delete network_troubleshoot_history.json as it's a persistent history
]
# Count of files deleted
deleted_count = 0
data_dir = 'data_temp'
# Delete each file if it exists
for filename in files_to_clean:
file_path = os.path.join(data_dir, filename)
if os.path.exists(file_path):
os.remove(file_path)
deleted_count += 1
print(f"Deleted {file_path}")
# Show confirmation message
if deleted_count > 0:
QMessageBox.information(
self,
"Cleanup Complete",
f"Successfully cleaned {deleted_count} temporary data files."
)
else:
QMessageBox.information(
self,
"Cleanup Complete",
"No temporary files found to clean."
)
except Exception as e:
print(f"Error cleaning temp files: {e}")
QMessageBox.warning(
self,
"Cleanup Warning",
f"Some files could not be deleted: {str(e)}"
)
def cleanup(self):
"""Clean up all resources and windows"""
try:
# List of all window attributes to clean up
windows = [
'monitoring_window',
'network_troubleshoot_window',
'network_device_scan_window',
'wifi_signals_window',
'packet_analyzer_window',
'report_window'
]
# Close and clean up each window
for window_name in windows:
if hasattr(self, window_name):
window = getattr(self, window_name)
if window:
try:
window.close()
window.deleteLater()
setattr(self, window_name, None)
except Exception as e:
print(f"Error cleaning up {window_name}: {str(e)}")
# Terminate app.py process if it's running
if hasattr(self, 'app_py_process') and self.app_py_process:
try:
if self.app_py_process.poll() is None: # Process is still running
print("Terminating app.py process...")
self.app_py_process.terminate()
# Give it a moment to terminate gracefully
import time
time.sleep(0.5)
# Force kill if still running
if self.app_py_process.poll() is None:
self.app_py_process.kill()
print("app.py process terminated")
self.app_py_process = None
except Exception as e:
print(f"Error terminating app.py process: {str(e)}")
# Reset window states
self.window_states = {
'packet_analyzer': False,
'network_device_scan': False,
'wifi_signals': False,
'network_troubleshoot': False
}
except Exception as e:
print(f"Error during cleanup: {str(e)}")
def closeEvent(self, event):
"""Handle window closing"""
try:
# Ask user if they want to clean temporary files
reply = QMessageBox.question(
self,
"Clean Temporary Files",
"Do you want to clean temporary data files before closing?\n\n"
"This will remove all saved data from your analysis sessions.",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
QMessageBox.StandardButton.No
)
if reply == QMessageBox.StandardButton.Yes:
self.clean_temp_files()
# Explicitly terminate app.py process if it exists
if hasattr(self, 'app_py_process') and self.app_py_process:
try:
if self.app_py_process.poll() is None: # Process is still running
print("Terminating app.py process from closeEvent...")
self.app_py_process.terminate()
# Give it a moment to terminate gracefully
import time
time.sleep(0.5)
# Force kill if still running
if self.app_py_process.poll() is None:
print("Force killing app.py process...")
self.app_py_process.kill()
print("app.py process terminated successfully")
self.app_py_process = None
except Exception as e:
print(f"Error terminating app.py process: {str(e)}")
# Run general cleanup
self.cleanup()
event.accept()
except Exception as e:
print(f"Error during window closing: {str(e)}")
event.accept()
def open_alert_system(self):
"""Open the Alert System window"""
try:
if not hasattr(self, 'alert_system_window') or self.alert_system_window is None:
from change_number import NumberTracker
self.alert_system_window = NumberTracker()
self.alert_system_window.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
self.alert_system_window.show()
self.alert_system_window.raise_()
except Exception as e:
print(f"Error opening Alert System: {str(e)}")
QMessageBox.critical(
self,
"Error",
f"Failed to open Alert System:\n{str(e)}\n\n"
"Please try again or restart the application."
)
def load_sms_alert_state(self):
"""Load the current SMS alert state from settings file"""
try:
settings_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'sms_alert_settings.json')
if os.path.exists(settings_file):
with open(settings_file, 'r') as f:
settings = json.load(f)
# Update button text and style based on current state
if settings.get('enabled', True):
self.sms_alert_btn.setText("SMS Alert: ON")
self.sms_alert_btn.setStyleSheet("""
QPushButton {
background-color: #4CAF50;
color: white;
border-radius: 3px;
padding: 3px 8px;
text-align: center;
min-height: 20px;
max-width: 100px;
}
QPushButton:hover {
background-color: #45a049;
}
QPushButton:pressed {
background-color: #3c8c40;
}
""")
else:
self.sms_alert_btn.setText("SMS Alert: OFF")
self.sms_alert_btn.setStyleSheet("""
QPushButton {
background-color: #f44336;
color: white;
border-radius: 3px;
padding: 3px 8px;
text-align: center;
min-height: 20px;
max-width: 100px;
}
QPushButton:hover {
background-color: #d32f2f;
}
QPushButton:pressed {
background-color: #b71c1c;
}
""")
else:
# Create default settings if file doesn't exist
with open(settings_file, 'w') as f:
json.dump({"enabled": True, "phone_number": "+94718830879", "alert_history": []}, f, indent=4)
self.sms_alert_btn.setText("SMS Alert: ON")
except Exception as e:
print(f"Error loading SMS alert state: {e}")
def toggle_sms_alert(self):
"""Toggle SMS alert system on/off"""
try:
settings_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'sms_alert_settings.json')
if os.path.exists(settings_file):
with open(settings_file, 'r') as f:
settings = json.load(f)
# Toggle the enabled state
settings['enabled'] = not settings.get('enabled', True)
# Save the updated settings
with open(settings_file, 'w') as f:
json.dump(settings, f, indent=4)
# Update button text and style
if settings['enabled']:
self.sms_alert_btn.setText("SMS Alert: ON")
self.sms_alert_btn.setStyleSheet("""
QPushButton {