-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsftp_browser.py
More file actions
969 lines (829 loc) · 34.7 KB
/
Copy pathsftp_browser.py
File metadata and controls
969 lines (829 loc) · 34.7 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
"""
sftp_browser.py - Pannello SFTP laterale stile MobaXterm (sidebar SSH browser)
Usa paramiko per navigare, scaricare e caricare file via SSH/SFTP.
"""
import os
import stat
import threading
import shutil
from pathlib import Path
from PyQt6.QtWidgets import (
QWidget, QVBoxLayout, QHBoxLayout, QListWidget, QListWidgetItem,
QPushButton, QLineEdit, QLabel, QMenu, QFileDialog, QMessageBox,
QProgressDialog, QApplication, QToolButton, QFrame, QStyle
)
from PyQt6.QtCore import Qt, pyqtSignal, QThread, QObject
from PyQt6.QtGui import QIcon, QFont
try:
import paramiko
import stat as stat_mod
PARAMIKO_OK = True
except ImportError:
PARAMIKO_OK = False
from translations import t
# ---------------------------------------------------------------------------
# Worker thread per operazioni SFTP bloccanti
# ---------------------------------------------------------------------------
class SftpWorker(QObject):
finished = pyqtSignal(bool, str) # successo, messaggio
progress = pyqtSignal(int) # percentuale 0-100
def __init__(self, sftp, operazione, src, dst, size=0):
super().__init__()
self._sftp = sftp
self._op = operazione # 'get' o 'put'
self._src = src
self._dst = dst
self._size = size
self._trasferito = 0
def run(self):
try:
def callback(tx, tot):
self._trasferito = tx
if tot > 0:
self.progress.emit(int(tx * 100 / tot))
if self._op == "get":
self._sftp.get(self._src, self._dst, callback=callback)
else:
self._sftp.put(self._src, self._dst, callback=callback)
self.finished.emit(True, "OK")
except Exception as e:
self.finished.emit(False, str(e))
class FtpWorker(QObject):
finished = pyqtSignal(bool, str)
def __init__(self, ftp, operazione, src, dst):
super().__init__()
self._ftp = ftp
self._op = operazione # 'get' o 'put'
self._src = src
self._dst = dst
def run(self):
try:
if self._op == "get":
with open(self._dst, "wb") as fp:
self._ftp.retrbinary(f"RETR {self._src}", fp.write)
else:
with open(self._src, "rb") as fp:
self._ftp.storbinary(f"STOR {self._dst}", fp)
self.finished.emit(True, "OK")
except Exception as e:
self.finished.emit(False, str(e))
# ---------------------------------------------------------------------------
# Widget principale
# ---------------------------------------------------------------------------
class SftpBrowserWidget(QWidget):
"""
Sidebar SFTP in stile MobaXterm:
- albero di navigazione file/cartelle
- toolbar con su/giù/refresh/home/upload/download
- drag & drop (futuro)
- menu contestuale tasto destro
"""
richiesta_cd = pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self._ssh = None
self._sftp = None
self._path_corrente = "/"
self._cronologia = []
self._connesso = False
self._init_ui()
# ------------------------------------------------------------------
# UI
# ------------------------------------------------------------------
def _init_ui(self):
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
# Header
hdr = QLabel(" 🗂 Browser SFTP")
hdr.setStyleSheet(
"background:#f0f0f0; color:#4e7abc; font-size:12px; font-weight:bold; "
"padding:6px; border-bottom:1px solid #ccc;"
)
layout.addWidget(hdr)
# Toolbar di navigazione
tb = QHBoxLayout()
tb.setContentsMargins(4, 2, 4, 2)
tb.setSpacing(2)
self.btn_su = self._mkbtn("⬆", t("sftp.tooltip_up"), self._vai_su)
self.btn_home = self._mkbtn("🏠", t("sftp.tooltip_home"), self._vai_home)
self.btn_refresh = self._mkbtn("↺", t("sftp.tooltip_refresh"), self._aggiorna)
self.btn_upload = self._mkbtn("⬆️", t("sftp.tooltip_upload"), self._carica_file)
self.btn_dl = self._mkbtn("⬇️", t("sftp.tooltip_download"),self._scarica_file)
self.btn_mkdir = self._mkbtn("📁+", t("sftp.tooltip_mkdir"), self._nuova_cartella)
for b in [self.btn_su, self.btn_home, self.btn_refresh,
self.btn_upload, self.btn_dl, self.btn_mkdir]:
tb.addWidget(b)
tb.addStretch()
layout.addLayout(tb)
sep = QFrame()
sep.setFrameShape(QFrame.Shape.HLine)
sep.setStyleSheet("color:#444;")
layout.addWidget(sep)
# Barra percorso
self.path_edit = QLineEdit("/")
self.path_edit.setReadOnly(True)
self.path_edit.setStyleSheet(
"background:#ffffff; color:#1a5ca8; font-family:monospace; "
"font-size:12px; border:none; border-bottom:1px solid #ccc; padding:3px 6px;"
)
layout.addWidget(self.path_edit)
# Lista file
self.lista = QListWidget()
self.lista.setFont(QFont("Monospace", 11))
self.lista.setStyleSheet(
"QListWidget { background:#ffffff; color:#111111; border:none; }"
"QListWidget::item { padding:3px 6px; }"
"QListWidget::item:selected { background:#4e7abc; color:#fff; }"
"QListWidget::item:hover:!selected { background:#e8eef5; }"
)
self.lista.itemDoubleClicked.connect(self._doppio_clic)
self.lista.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.lista.customContextMenuRequested.connect(self._menu_contestuale)
layout.addWidget(self.lista, 1)
# Status bar
self.status = QLabel(" Non connesso")
self.status.setStyleSheet(
"background:#f0f0f0; color:#555555; font-size:11px; padding:3px 6px; border-top:1px solid #ccc;"
)
layout.addWidget(self.status)
self._imposta_disconnesso()
def _mkbtn(self, label, tooltip, slot):
b = QToolButton()
b.setText(label)
b.setToolTip(tooltip)
b.setFixedHeight(24)
b.setStyleSheet(
"QToolButton { background:transparent; color:#444444; border:none; "
"font-size:13px; padding:0 3px; }"
"QToolButton:hover { color:#fff; background:#4e7abc; border-radius:3px; }"
"QToolButton:disabled { color:#aaaaaa; }"
)
b.clicked.connect(slot)
return b
# ------------------------------------------------------------------
# Connessione / disconnessione
# ------------------------------------------------------------------
def connetti(self, host, port, user, pwd, pkey_path=""):
if not PARAMIKO_OK:
self._set_status("⚠ paramiko non installato: pip install paramiko")
return
self._disconnetti_silenzioso()
self._set_status(t("sftp.connecting"))
self.lista.clear()
try:
self._ssh = paramiko.SSHClient()
self._ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
kw = dict(hostname=host, port=int(port), username=user, timeout=8)
if pkey_path and os.path.exists(pkey_path):
kw["key_filename"] = pkey_path
elif pwd:
kw["password"] = pwd
self._ssh.connect(**kw)
self._sftp = self._ssh.open_sftp()
self._path_corrente = self._sftp.normalize(".")
self._connesso = True
self._carica_cartella(self._path_corrente)
self._set_status(f"✔ {user}@{host}:{port}")
self._imposta_connesso()
except Exception as e:
self._set_status(t("sftp.err_prefix").format(msg=str(e)))
self._imposta_disconnesso()
def disconnetti(self):
self._disconnetti_silenzioso()
self._imposta_disconnesso()
self.lista.clear()
self.path_edit.setText("/")
self._set_status(" Non connesso")
def _disconnetti_silenzioso(self):
try:
if self._sftp:
self._sftp.close()
if self._ssh:
self._ssh.close()
except Exception:
pass
self._sftp = None
self._ssh = None
self._connesso = False
# ------------------------------------------------------------------
# Navigazione
# ------------------------------------------------------------------
def _carica_cartella(self, path):
if not self._sftp:
return
try:
voci = self._sftp.listdir_attr(path)
self.lista.clear()
# Prima le cartelle poi i file, ordine alfabetico
cartelle = sorted(
[v for v in voci if stat.S_ISDIR(v.st_mode)],
key=lambda x: x.filename.lower()
)
file = sorted(
[v for v in voci if not stat.S_ISDIR(v.st_mode)],
key=lambda x: x.filename.lower()
)
_ico_dir = QApplication.style().standardIcon(QStyle.StandardPixmap.SP_DirIcon)
_ico_file = QApplication.style().standardIcon(QStyle.StandardPixmap.SP_FileIcon)
# ".." per salire
item_su = QListWidgetItem(_ico_dir, "..")
item_su.setData(Qt.ItemDataRole.UserRole, "..")
self.lista.addItem(item_su)
for v in cartelle:
item = QListWidgetItem(_ico_dir, v.filename)
item.setData(Qt.ItemDataRole.UserRole, ("dir", v.filename))
item.setForeground(QColor("#1a5ca8"))
self.lista.addItem(item)
for v in file:
size_str = self._fmt_size(v.st_size)
item = QListWidgetItem(_ico_file, f"{v.filename} ({size_str})")
item.setData(Qt.ItemDataRole.UserRole, ("file", v.filename))
self.lista.addItem(item)
self._path_corrente = path
self.path_edit.setText(path)
self._cronologia.append(path)
except Exception as e:
self._set_status(f"✖ {e}")
def _doppio_clic(self, item):
data = item.data(Qt.ItemDataRole.UserRole)
if data == "..":
self._vai_su()
elif isinstance(data, tuple):
tipo, nome = data
if tipo == "dir":
nuovo = self._path_corrente.rstrip("/") + "/" + nome
self._carica_cartella(nuovo)
elif tipo == "file":
self._scarica_file_singolo(nome)
def _vai_su(self):
parent = str(Path(self._path_corrente).parent)
if parent != self._path_corrente:
self._carica_cartella(parent)
def _vai_home(self):
if self._sftp:
try:
home = self._sftp.normalize(".")
self._carica_cartella(home)
except Exception:
pass
def _aggiorna(self):
if self._connesso:
self._carica_cartella(self._path_corrente)
# ------------------------------------------------------------------
# Upload / Download
# ------------------------------------------------------------------
def _scarica_file(self):
item = self.lista.currentItem()
if not item:
return
data = item.data(Qt.ItemDataRole.UserRole)
if isinstance(data, tuple) and data[0] == "file":
self._scarica_file_singolo(data[1])
def _scarica_file_singolo(self, nome):
if not self._sftp:
return
dst, _ = QFileDialog.getSaveFileName(self, t("sftp.download_title"), nome)
if not dst:
return
src = self._path_corrente.rstrip("/") + "/" + nome
try:
size = self._sftp.stat(src).st_size or 0
except Exception:
size = 0
progress = QProgressDialog(t("sftp.downloading").format(name=nome), t("sftp.download_cancel"), 0, 100, self)
progress.setWindowModality(Qt.WindowModality.WindowModal)
progress.setMinimumDuration(300)
self._set_status(f"⬇ Download {nome}…")
worker = SftpWorker(self._sftp, "get", src, dst, size)
thread = QThread(self)
worker.moveToThread(thread)
thread.started.connect(worker.run)
worker.progress.connect(progress.setValue)
worker.finished.connect(
lambda ok, msg, n=nome, p=progress, thr=thread:
self._on_sftp_done(ok, msg, n, t("sftp.downloaded"), p, thr, refresh=False)
)
thread.start()
def _carica_file(self):
if not self._sftp:
return
files, _ = QFileDialog.getOpenFileNames(self, "Seleziona file da caricare")
if files:
self._carica_lista(list(files))
def _carica_lista(self, files: list):
if not files:
self._aggiorna()
return
local_path = files[0]
rimanenti = files[1:]
nome = os.path.basename(local_path)
remote_path = self._path_corrente.rstrip("/") + "/" + nome
progress = QProgressDialog(f"⬆ Upload: {nome}", "Annulla", 0, 100, self)
progress.setWindowModality(Qt.WindowModality.WindowModal)
progress.setMinimumDuration(300)
self._set_status(f"⬆ Upload {nome}…")
worker = SftpWorker(self._sftp, "put", local_path, remote_path)
thread = QThread(self)
worker.moveToThread(thread)
thread.started.connect(worker.run)
worker.progress.connect(progress.setValue)
worker.finished.connect(
lambda ok, msg, n=nome, p=progress, t=thread, r=rimanenti:
self._on_sftp_upload_done(ok, msg, n, p, t, r)
)
thread.start()
def _on_sftp_upload_done(self, ok, msg, nome, progress, thread, rimanenti):
progress.close()
thread.quit()
thread.wait()
if ok:
self._set_status(f"✔ Caricato: {nome}")
else:
QMessageBox.critical(self, "Errore upload", msg)
self._set_status(f"✖ Errore: {msg}")
self._carica_lista(rimanenti)
def _on_sftp_done(self, ok, msg, nome, azione, progress, thread, refresh=False):
progress.close()
thread.quit()
thread.wait()
if ok:
self._set_status(f"✔ {azione}: {nome}")
else:
QMessageBox.critical(self, f"Errore {azione.lower()}", msg)
self._set_status(f"✖ Errore: {msg}")
if refresh:
self._aggiorna()
# ------------------------------------------------------------------
# Operazioni file
# ------------------------------------------------------------------
def _nuova_cartella(self):
if not self._sftp:
return
from PyQt6.QtWidgets import QInputDialog
nome, ok = QInputDialog.getText(self, "Nuova cartella", "Nome:")
if ok and nome:
path = self._path_corrente.rstrip("/") + "/" + nome
try:
self._sftp.mkdir(path)
self._aggiorna()
except Exception as e:
QMessageBox.critical(self, "Errore", str(e))
def _rinomina(self, nome_orig):
if not self._sftp:
return
from PyQt6.QtWidgets import QInputDialog
nuovo, ok = QInputDialog.getText(self, "Rinomina", "Nuovo nome:", text=nome_orig)
if ok and nuovo and nuovo != nome_orig:
src = self._path_corrente.rstrip("/") + "/" + nome_orig
dst = self._path_corrente.rstrip("/") + "/" + nuovo
try:
self._sftp.rename(src, dst)
self._aggiorna()
except Exception as e:
QMessageBox.critical(self, "Errore rinomina", str(e))
def _elimina(self, nome, is_dir):
if not self._sftp:
return
risposta = QMessageBox.question(
self, "Elimina",
f"Eliminare {'la cartella' if is_dir else 'il file'} '{nome}'?"
)
if risposta != QMessageBox.StandardButton.Yes:
return
path = self._path_corrente.rstrip("/") + "/" + nome
try:
if is_dir:
self._sftp.rmdir(path)
else:
self._sftp.remove(path)
self._aggiorna()
except Exception as e:
QMessageBox.critical(self, "Errore eliminazione", str(e))
# ------------------------------------------------------------------
# Menu contestuale
# ------------------------------------------------------------------
def _menu_contestuale(self, pos):
item = self.lista.itemAt(pos)
menu = QMenu(self)
menu.setStyleSheet(
"QMenu { background:#ffffff; color:#111111; border:1px solid #ccc; }"
"QMenu::item:selected { background:#4e7abc; color:#fff; }"
)
if item:
data = item.data(Qt.ItemDataRole.UserRole)
if isinstance(data, tuple):
tipo, nome = data
is_dir = tipo == "dir"
if not is_dir:
menu.addAction("⬇ Scarica", lambda: self._scarica_file_singolo(nome))
menu.addAction("✏ Rinomina", lambda: self._rinomina(nome))
menu.addAction("🗑 Elimina", lambda: self._elimina(nome, is_dir))
menu.addSeparator()
menu.addAction("⬆ Carica file", self._carica_file)
menu.addAction("📁+ Nuova cartella", self._nuova_cartella)
menu.addSeparator()
menu.addAction("↺ Aggiorna", self._aggiorna)
menu.exec(self.lista.mapToGlobal(pos))
# ------------------------------------------------------------------
# Stato UI
# ------------------------------------------------------------------
def _imposta_connesso(self):
for b in [self.btn_su, self.btn_home, self.btn_refresh,
self.btn_upload, self.btn_dl, self.btn_mkdir]:
b.setEnabled(True)
def _imposta_disconnesso(self):
for b in [self.btn_su, self.btn_home, self.btn_refresh,
self.btn_upload, self.btn_dl, self.btn_mkdir]:
b.setEnabled(False)
def _set_status(self, msg):
self.status.setText(f" {msg}")
# ------------------------------------------------------------------
# Utility
# ------------------------------------------------------------------
@staticmethod
def _fmt_size(n: int) -> str:
for unit in ("B", "KB", "MB", "GB"):
if n < 1024:
return f"{n:.0f} {unit}"
n /= 1024
return f"{n:.1f} TB"
# ---------------------------------------------------------------------------
# FTP Browser Widget (ftplib — stdlib, nessuna dipendenza esterna)
# ---------------------------------------------------------------------------
import ftplib
from PyQt6.QtGui import QColor
class FtpBrowserWidget(QWidget):
"""
Sidebar FTP in stile SftpBrowserWidget.
Usa ftplib (stdlib) — supporta FTP plain e FTPS (TLS esplicito).
Funzionalità: navigazione, download, upload, nuova cartella, rinomina, elimina.
"""
def __init__(self, parent=None):
super().__init__(parent)
self._ftp: ftplib.FTP | None = None
self._path_corrente = "/"
self._cronologia: list[str] = []
self._connesso = False
self._init_ui()
# ------------------------------------------------------------------
# UI (identica a SftpBrowserWidget)
# ------------------------------------------------------------------
def _init_ui(self):
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.setSpacing(0)
hdr = QLabel(" 🗂 Browser FTP")
hdr.setStyleSheet(
"background:#f0f0f0; color:#b87a00; font-size:12px; font-weight:bold; "
"padding:6px; border-bottom:1px solid #ccc;"
)
layout.addWidget(hdr)
tb = QHBoxLayout()
tb.setContentsMargins(4, 2, 4, 2)
tb.setSpacing(2)
self.btn_su = self._mkbtn("⬆", "Cartella superiore", self._vai_su)
self.btn_home = self._mkbtn("🏠", "Home directory", self._vai_home)
self.btn_refresh = self._mkbtn("↺", "Aggiorna", self._aggiorna)
self.btn_upload = self._mkbtn("⬆️", "Carica file", self._carica_file)
self.btn_dl = self._mkbtn("⬇️", "Scarica file", self._scarica_file)
self.btn_mkdir = self._mkbtn("📁+","Nuova cartella", self._nuova_cartella)
for b in [self.btn_su, self.btn_home, self.btn_refresh,
self.btn_upload, self.btn_dl, self.btn_mkdir]:
tb.addWidget(b)
tb.addStretch()
layout.addLayout(tb)
sep = QFrame()
sep.setFrameShape(QFrame.Shape.HLine)
sep.setStyleSheet("color:#ccc;")
layout.addWidget(sep)
self.path_edit = QLineEdit("/")
self.path_edit.setReadOnly(True)
self.path_edit.setStyleSheet(
"background:#ffffff; color:#b87a00; font-family:monospace; "
"font-size:12px; border:none; border-bottom:1px solid #ccc; padding:3px 6px;"
)
layout.addWidget(self.path_edit)
self.lista = QListWidget()
self.lista.setFont(QFont("Monospace", 11))
self.lista.setStyleSheet(
"QListWidget { background:#ffffff; color:#111111; border:none; }"
"QListWidget::item { padding:3px 6px; }"
"QListWidget::item:selected { background:#b87a00; color:#fff; }"
"QListWidget::item:hover:!selected { background:#fdf3e0; }"
)
self.lista.itemDoubleClicked.connect(self._doppio_clic)
self.lista.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
self.lista.customContextMenuRequested.connect(self._menu_contestuale)
layout.addWidget(self.lista, 1)
self.status = QLabel(" Non connesso")
self.status.setStyleSheet(
"background:#f0f0f0; color:#555555; font-size:11px; padding:3px 6px; border-top:1px solid #ccc;"
)
layout.addWidget(self.status)
self._imposta_disconnesso()
def _mkbtn(self, label, tooltip, slot):
b = QToolButton()
b.setText(label)
b.setToolTip(tooltip)
b.setFixedHeight(24)
b.setStyleSheet(
"QToolButton { background:transparent; color:#444444; border:none; "
"font-size:13px; padding:0 3px; }"
"QToolButton:hover { color:#fff; background:#b87a00; border-radius:3px; }"
"QToolButton:disabled { color:#aaaaaa; }"
)
b.clicked.connect(slot)
return b
# ------------------------------------------------------------------
# Connessione
# ------------------------------------------------------------------
def connetti(self, host: str, port: int | str, user: str, pwd: str,
tls: bool = False, passive: bool = True):
"""
Apre la connessione FTP (o FTPS con TLS esplicito se tls=True).
passive=True → modalità passiva (PASV) — default, funziona dietro NAT.
passive=False → modalità attiva (PORT) — solo reti senza firewall.
"""
self._disconnetti_silenzioso()
self._set_status("⏳ Connessione FTP in corso…")
self.lista.clear()
try:
port = int(port) if port else (21 if not tls else 990)
if tls:
ftp = ftplib.FTP_TLS(timeout=10)
ftp.connect(host, port)
ftp.auth() # negozia TLS
ftp.prot_p() # dati cifrati
else:
ftp = ftplib.FTP(timeout=10)
ftp.connect(host, port)
ftp.login(user or "anonymous", pwd or "")
ftp.set_pasv(passive)
self._ftp = ftp
self._connesso = True
self._path_corrente = self._ftp.pwd()
self._carica_cartella(self._path_corrente)
proto_str = "FTPS" if tls else "FTP"
self._set_status(f"✔ {proto_str} {user}@{host}:{port}")
self._imposta_connesso()
except Exception as e:
self._set_status(f"✖ Errore FTP: {e}")
self._imposta_disconnesso()
def disconnetti(self):
self._disconnetti_silenzioso()
self._imposta_disconnesso()
self.lista.clear()
self.path_edit.setText("/")
self._set_status(" Non connesso")
def _disconnetti_silenzioso(self):
try:
if self._ftp:
self._ftp.quit()
except Exception:
try:
if self._ftp:
self._ftp.close()
except Exception:
pass
self._ftp = None
self._connesso = False
# ------------------------------------------------------------------
# Navigazione
# ------------------------------------------------------------------
def _carica_cartella(self, path: str):
if not self._ftp:
return
try:
self._ftp.cwd(path)
self._path_corrente = self._ftp.pwd()
self.path_edit.setText(self._path_corrente)
# MLSD (RFC 3659) se disponibile, fallback LIST
voci = []
try:
for nome, fatti in self._ftp.mlsd():
if nome in (".", ".."):
continue
tipo = fatti.get("type", "file")
size = int(fatti.get("size", 0))
voci.append((nome, tipo == "dir", size))
except ftplib.error_perm:
# Server non supporta MLSD → parse LIST grezzo
righe: list[str] = []
self._ftp.retrlines("LIST", righe.append)
for riga in righe:
parti = riga.split(None, 8)
if len(parti) < 9:
continue
is_dir = riga.startswith("d")
nome = parti[8].strip()
if nome in (".", ".."):
continue
try:
size = int(parti[4])
except ValueError:
size = 0
voci.append((nome, is_dir, size))
self.lista.clear()
_ico_dir = QApplication.style().standardIcon(QStyle.StandardPixmap.SP_DirIcon)
_ico_file = QApplication.style().standardIcon(QStyle.StandardPixmap.SP_FileIcon)
# ".." per salire
item_su = QListWidgetItem(_ico_dir, "..")
item_su.setData(Qt.ItemDataRole.UserRole, "..")
self.lista.addItem(item_su)
cartelle = sorted([(n, s) for n, d, s in voci if d], key=lambda x: x[0].lower())
file_voci = sorted([(n, s) for n, d, s in voci if not d], key=lambda x: x[0].lower())
for nome, _ in cartelle:
item = QListWidgetItem(_ico_dir, nome)
item.setData(Qt.ItemDataRole.UserRole, ("dir", nome))
item.setForeground(QColor("#b87a00"))
self.lista.addItem(item)
for nome, size in file_voci:
item = QListWidgetItem(_ico_file, f"{nome} ({SftpBrowserWidget._fmt_size(size)})")
item.setData(Qt.ItemDataRole.UserRole, ("file", nome))
self.lista.addItem(item)
self._cronologia.append(self._path_corrente)
self._set_status(
f" {len(cartelle)} cartelle, {len(file_voci)} file — {self._path_corrente}"
)
except Exception as e:
self._set_status(f"✖ {e}")
def _doppio_clic(self, item):
data = item.data(Qt.ItemDataRole.UserRole)
if data == "..":
self._vai_su()
elif isinstance(data, tuple):
tipo, nome = data
if tipo == "dir":
nuovo = self._path_corrente.rstrip("/") + "/" + nome
self._carica_cartella(nuovo)
elif tipo == "file":
self._scarica_file_singolo(nome)
def _vai_su(self):
from pathlib import Path
parent = str(Path(self._path_corrente).parent)
if parent != self._path_corrente:
self._carica_cartella(parent)
def _vai_home(self):
if self._ftp:
try:
self._carica_cartella("/")
except Exception:
pass
def _aggiorna(self):
if self._connesso:
self._carica_cartella(self._path_corrente)
# ------------------------------------------------------------------
# Upload / Download
# ------------------------------------------------------------------
def _scarica_file(self):
item = self.lista.currentItem()
if not item:
return
data = item.data(Qt.ItemDataRole.UserRole)
if isinstance(data, tuple) and data[0] == "file":
self._scarica_file_singolo(data[1])
def _scarica_file_singolo(self, nome: str):
if not self._ftp:
return
dst, _ = QFileDialog.getSaveFileName(self, t("sftp.download_title"), nome)
if not dst:
return
remote = self._path_corrente.rstrip("/") + "/" + nome
self._set_status(f"⬇ Download {nome}…")
worker = FtpWorker(self._ftp, "get", remote, dst)
thread = QThread(self)
worker.moveToThread(thread)
thread.started.connect(worker.run)
worker.finished.connect(
lambda ok, msg, n=nome, t=thread:
self._on_ftp_done(ok, msg, n, "Scaricato", t, refresh=False)
)
thread.start()
def _carica_file(self):
if not self._ftp:
return
files, _ = QFileDialog.getOpenFileNames(self, "Seleziona file da caricare")
if files:
self._carica_ftp_lista(list(files))
def _carica_ftp_lista(self, files: list):
if not files:
self._aggiorna()
return
local_path = files[0]
rimanenti = files[1:]
nome = os.path.basename(local_path)
remote = self._path_corrente.rstrip("/") + "/" + nome
self._set_status(f"⬆ Upload {nome}…")
worker = FtpWorker(self._ftp, "put", local_path, remote)
thread = QThread(self)
worker.moveToThread(thread)
thread.started.connect(worker.run)
worker.finished.connect(
lambda ok, msg, n=nome, t=thread, r=rimanenti:
self._on_ftp_upload_done(ok, msg, n, t, r)
)
thread.start()
def _on_ftp_upload_done(self, ok, msg, nome, thread, rimanenti):
thread.quit()
thread.wait()
if ok:
self._set_status(f"✔ Caricato: {nome}")
else:
QMessageBox.critical(self, "Errore upload", msg)
self._set_status(f"✖ Errore: {msg}")
self._carica_ftp_lista(rimanenti)
def _on_ftp_done(self, ok, msg, nome, azione, thread, refresh=False):
thread.quit()
thread.wait()
if ok:
self._set_status(f"✔ {azione}: {nome}")
else:
QMessageBox.critical(self, f"Errore {azione.lower()}", msg)
self._set_status(f"✖ Errore: {msg}")
if refresh:
self._aggiorna()
# ------------------------------------------------------------------
# Operazioni file
# ------------------------------------------------------------------
def _nuova_cartella(self):
if not self._ftp:
return
from PyQt6.QtWidgets import QInputDialog
nome, ok = QInputDialog.getText(self, "Nuova cartella", "Nome:")
if ok and nome:
path = self._path_corrente.rstrip("/") + "/" + nome
try:
self._ftp.mkd(path)
self._aggiorna()
except Exception as e:
QMessageBox.critical(self, "Errore", str(e))
def _rinomina(self, nome_orig: str):
if not self._ftp:
return
from PyQt6.QtWidgets import QInputDialog
nuovo, ok = QInputDialog.getText(self, "Rinomina", "Nuovo nome:", text=nome_orig)
if ok and nuovo and nuovo != nome_orig:
src = self._path_corrente.rstrip("/") + "/" + nome_orig
dst = self._path_corrente.rstrip("/") + "/" + nuovo
try:
self._ftp.rename(src, dst)
self._aggiorna()
except Exception as e:
QMessageBox.critical(self, "Errore rinomina", str(e))
def _elimina(self, nome: str, is_dir: bool):
if not self._ftp:
return
risposta = QMessageBox.question(
self, "Elimina",
f"Eliminare {'la cartella' if is_dir else 'il file'} '{nome}'?"
)
if risposta != QMessageBox.StandardButton.Yes:
return
path = self._path_corrente.rstrip("/") + "/" + nome
try:
if is_dir:
self._ftp.rmd(path)
else:
self._ftp.delete(path)
self._aggiorna()
except Exception as e:
QMessageBox.critical(self, "Errore eliminazione", str(e))
# ------------------------------------------------------------------
# Menu contestuale
# ------------------------------------------------------------------
def _menu_contestuale(self, pos):
item = self.lista.itemAt(pos)
menu = QMenu(self)
menu.setStyleSheet(
"QMenu { background:#ffffff; color:#111111; border:1px solid #ccc; }"
"QMenu::item:selected { background:#b87a00; color:#fff; }"
)
if item:
data = item.data(Qt.ItemDataRole.UserRole)
if isinstance(data, tuple):
tipo, nome = data
is_dir = tipo == "dir"
if not is_dir:
menu.addAction("⬇ Scarica", lambda: self._scarica_file_singolo(nome))
menu.addAction("✏ Rinomina", lambda: self._rinomina(nome))
menu.addAction("🗑 Elimina", lambda: self._elimina(nome, is_dir))
menu.addSeparator()
menu.addAction("⬆ Carica file", self._carica_file)
menu.addAction("📁+ Nuova cartella", self._nuova_cartella)
menu.addSeparator()
menu.addAction("↺ Aggiorna", self._aggiorna)
menu.exec(self.lista.mapToGlobal(pos))
# ------------------------------------------------------------------
# Stato UI
# ------------------------------------------------------------------
def _imposta_connesso(self):
for b in [self.btn_su, self.btn_home, self.btn_refresh,
self.btn_upload, self.btn_dl, self.btn_mkdir]:
b.setEnabled(True)
def _imposta_disconnesso(self):
for b in [self.btn_su, self.btn_home, self.btn_refresh,
self.btn_upload, self.btn_dl, self.btn_mkdir]:
b.setEnabled(False)
def _set_status(self, msg: str):
self.status.setText(f" {msg}")