This file is indexed.

/usr/share/backintime/qt4/app.py is in backintime-qt4 1.1.12-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
#    Back In Time
#    Copyright (C) 2008-2016 Oprea Dan, Bart de Koning, Richard Bailey, Germar Reitze
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


import os
import sys

if not os.getenv( 'DISPLAY', '' ):
    os.putenv( 'DISPLAY', ':0.0' )

import datetime
import gettext
import re
import subprocess

import qt4tools
qt4tools.register_backintime_path('common')

import backintime
import tools
import logger
import snapshots
import guiapplicationinstance
import mount
import progress
from exceptions import MountException

from PyQt4.QtGui import *
from PyQt4.QtCore import *

import settingsdialog
import snapshotsdialog
import logviewdialog
import restoredialog
import messagebox


_=gettext.gettext


class MainWindow( QMainWindow ):
    def __init__( self, config, app_instance, qapp ):
        QMainWindow.__init__( self )

        self.config = config
        self.app_instance = app_instance
        self.qapp = qapp
        self.snapshots = snapshots.Snapshots( config )
        self.last_take_snapshot_message = None

        #window icon
        import icon
        self.qapp.setWindowIcon(icon.BIT_LOGO)

        #main toolbar
        self.main_toolbar = self.addToolBar('main')
        self.main_toolbar.setFloatable( False )

        #profiles
        self.first_update_all = True
        self.disable_profile_changed = False
        self.combo_profiles = QComboBox( self )
        self.combo_profiles_action = self.main_toolbar.addWidget( self.combo_profiles )

        self.btn_take_snapshot = self.main_toolbar.addAction(icon.TAKE_SNAPSHOT, _('Take snapshot'))
        QObject.connect( self.btn_take_snapshot, SIGNAL('triggered()'), self.on_btn_take_snapshot_clicked )

        self.btn_update_snapshots = self.main_toolbar.addAction(icon.REFRESH_SNAPSHOT, _('Refresh snapshots list'))
        self.btn_update_snapshots.setShortcuts([Qt.Key_F5, QKeySequence(Qt.CTRL + Qt.Key_R)])
        QObject.connect( self.btn_update_snapshots, SIGNAL('triggered()'), self.on_btn_update_snapshots_clicked )

        self.btn_name_snapshot = self.main_toolbar.addAction(icon.SNAPSHOT_NAME, _('Snapshot Name'))
        QObject.connect( self.btn_name_snapshot, SIGNAL('triggered()'), self.on_btn_name_snapshot_clicked )

        self.btn_remove_snapshot = self.main_toolbar.addAction(icon.REMOVE_SNAPSHOT, _('Remove Snapshot'))
        QObject.connect( self.btn_remove_snapshot, SIGNAL('triggered()'), self.on_btn_remove_snapshot_clicked )

        self.btn_snapshot_log_view = self.main_toolbar.addAction(icon.VIEW_SNAPSHOT_LOG, _('View Snapshot Log'))
        QObject.connect( self.btn_snapshot_log_view, SIGNAL('triggered()'), self.on_btn_snapshot_log_view_clicked )

        self.btn_log_view = self.main_toolbar.addAction(icon.VIEW_LAST_LOG, _('View Last Log'))
        QObject.connect( self.btn_log_view, SIGNAL('triggered()'), self.on_btn_log_view_clicked )

        self.main_toolbar.addSeparator()

        self.btn_settings = self.main_toolbar.addAction(icon.SETTINGS, _('Settings'))
        QObject.connect( self.btn_settings, SIGNAL('triggered()'), self.on_btn_settings_clicked )

        self.main_toolbar.addSeparator()

        self.btn_shutdown = self.main_toolbar.addAction(icon.SHUTDOWN, _('Shutdown'))
        self.btn_shutdown.setToolTip(_('Shutdown system after snapshot has finished.'))
        self.btn_shutdown.setCheckable(True)
        self.shutdown = tools.ShutDown()
        self.btn_shutdown.setEnabled(self.shutdown.can_shutdown())
        QObject.connect( self.btn_shutdown, SIGNAL('toggled(bool)'), self.on_btn_shutdown_toggled )

        self.btn_quit = self.main_toolbar.addAction(icon.EXIT, _('Exit'))
        self.btn_quit.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_W))
        QObject.connect( self.btn_quit, SIGNAL('triggered()'), self.close )

        empty = QWidget(self)
        empty.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
        self.main_toolbar.addWidget(empty)

        help_menu = QMenu(self)
        self.btn_help = help_menu.addAction(icon.HELP, _('Help') )
        QObject.connect( self.btn_help, SIGNAL('triggered()'), self.on_help )
        self.btn_help_config = help_menu.addAction(icon.HELP, _('Config File Help'))
        QObject.connect(self.btn_help_config, SIGNAL('triggered()'), self.on_help_config)
        help_menu.addSeparator()
        self.btn_website = help_menu.addAction(icon.WEBSITE, _('Website') )
        QObject.connect( self.btn_website, SIGNAL('triggered()'), self.on_website)
        self.btn_changelog = help_menu.addAction(icon.CHANGELOG, _('Changelog') )
        QObject.connect( self.btn_changelog, SIGNAL('triggered()'), self.on_changelog)
        self.btn_faq = help_menu.addAction(icon.FAQ, _('FAQ') )
        QObject.connect( self.btn_faq, SIGNAL('triggered()'), self.on_faq)
        self.btn_question = help_menu.addAction(icon.QUESTION, _('Ask a question') )
        QObject.connect( self.btn_question, SIGNAL('triggered()'), self.on_ask_a_question)
        self.btn_bug = help_menu.addAction(icon.BUG, _('Report a bug') )
        QObject.connect( self.btn_bug, SIGNAL('triggered()'), self.on_report_a_bug)
        help_menu.addSeparator()
        self.btn_about = help_menu.addAction(icon.ABOUT, _('About') )
        QObject.connect( self.btn_about, SIGNAL('triggered()'), self.on_about)

        action = self.main_toolbar.addAction(icon.HELP, _('Help') )
        QObject.connect( action, SIGNAL('triggered()'), self.on_help )
        action.setMenu(help_menu)

        #main splitter
        self.main_splitter = QSplitter( self )
        self.main_splitter.setOrientation( Qt.Horizontal )

        #timeline
        self.list_time_line = qt4tools.TimeLine(self)
        self.main_splitter.addWidget(self.list_time_line)
        self.list_time_line.updateFilesView.connect(self.update_files_view)

        #right widget
        self.right_widget = QGroupBox( self )
        self.main_splitter.addWidget( self.right_widget )
        right_layout = QVBoxLayout( self.right_widget )
        left, top, right, bottom = right_layout.getContentsMargins()
        right_layout.setContentsMargins( 0, 0, right, 0 )

        #files toolbar
        self.files_view_toolbar = QToolBar( self )
        self.files_view_toolbar.setFloatable( False )

        self.btn_folder_up = self.files_view_toolbar.addAction(icon.UP, _('Up'))
        self.btn_folder_up.setShortcuts([QKeySequence(Qt.ALT + Qt.Key_Up), Qt.Key_Backspace])
        QObject.connect( self.btn_folder_up, SIGNAL('triggered()'), self.on_btn_folder_up_clicked )

        self.edit_current_path = QLineEdit( self )
        self.edit_current_path.setReadOnly( True )
        self.files_view_toolbar.addWidget( self.edit_current_path )

        #show hidden files
        self.show_hidden_files = self.config.get_bool_value( 'qt4.show_hidden_files', False )

        self.btn_show_hidden_files = self.files_view_toolbar.addAction(icon.SHOW_HIDDEN, _('Show hidden files'))
        self.btn_show_hidden_files.setShortcut(QKeySequence(Qt.CTRL + Qt.Key_H))
        self.btn_show_hidden_files.setCheckable( True )
        self.btn_show_hidden_files.setChecked( self.show_hidden_files )
        QObject.connect( self.btn_show_hidden_files, SIGNAL('toggled(bool)'), self.on_btn_show_hidden_files_toggled )

        self.files_view_toolbar.addSeparator()

        #restore menu
        self.menu_restore = QMenu(self)
        self.btn_restore = self.menu_restore.addAction(icon.RESTORE, _('Restore') )
        QObject.connect( self.btn_restore, SIGNAL('triggered()'), self.restore_this )
        self.btn_restore_to = self.menu_restore.addAction(icon.RESTORE_TO, _('Restore to ...') )
        QObject.connect( self.btn_restore_to, SIGNAL('triggered()'), self.restore_this_to )
        self.menu_restore.addSeparator()
        self.menu_restore_parent = self.menu_restore.addAction(icon.RESTORE, '' )
        QObject.connect( self.menu_restore_parent, SIGNAL('triggered()'), self.restore_parent )
        self.menu_restore_parent_to = self.menu_restore.addAction(icon.RESTORE_TO, '' )
        QObject.connect( self.menu_restore_parent_to, SIGNAL('triggered()'), self.restore_parent_to )
        self.menu_restore.addSeparator()
        self.btn_restore_delete = self.menu_restore.addAction(icon.RESTORE, _('Restore and delete new files'))
        QObject.connect(self.btn_restore_delete, SIGNAL('triggered()'), lambda: self.restore_this(True))
        self.menu_restore_parent_delete = self.menu_restore.addAction(icon.RESTORE, '')
        QObject.connect(self.menu_restore_parent_delete, SIGNAL('triggered()'), lambda: self.restore_parent(True))

        self.btn_restore_menu = self.files_view_toolbar.addAction(icon.RESTORE, _('Restore'))
        self.btn_restore_menu.setMenu(self.menu_restore)
        self.btn_restore_menu.setToolTip( _('Restore selected file or folder.\n'
                                            'If this button is grayed out this is most likely '
                                            'because "%(now)s" is selected in left hand snapshots list.') % {'now': _('Now')} )
        QObject.connect( self.btn_restore_menu, SIGNAL('triggered()'), self.restore_this )

        self.btn_snapshots = self.files_view_toolbar.addAction(icon.SNAPSHOTS, _('Snapshots'))
        QObject.connect( self.btn_snapshots, SIGNAL('triggered()'), self.on_btn_snapshots_clicked )

        right_layout.addWidget( self.files_view_toolbar )

        #menubar
        self.menubar = self.menuBar()
        self.menubar_snapshot = self.menubar.addMenu(_('Snapshot'))
        self.menubar_snapshot.addAction(self.btn_take_snapshot)
        self.menubar_snapshot.addAction(self.btn_update_snapshots)
        self.menubar_snapshot.addAction(self.btn_name_snapshot)
        self.menubar_snapshot.addAction(self.btn_remove_snapshot)
        self.menubar_snapshot.addSeparator()
        self.menubar_snapshot.addAction(self.btn_settings)
        self.menubar_snapshot.addSeparator()
        self.menubar_snapshot.addAction(self.btn_shutdown)
        self.menubar_snapshot.addAction(self.btn_quit)

        self.menubar_view = self.menubar.addMenu(_('View'))
        self.menubar_view.addAction(self.btn_folder_up)
        self.menubar_view.addAction(self.btn_show_hidden_files)
        self.menubar_view.addSeparator()
        self.menubar_view.addAction(self.btn_snapshot_log_view)
        self.menubar_view.addAction(self.btn_log_view)
        self.menubar_view.addSeparator()
        self.menubar_view.addAction(self.btn_snapshots)

        self.menubar_restore = self.menubar.addMenu(_('Restore'))
        self.menubar_restore.addAction(self.btn_restore)
        self.menubar_restore.addAction(self.btn_restore_to)
        self.menubar_restore.addSeparator()
        self.menubar_restore.addAction(self.menu_restore_parent)
        self.menubar_restore.addAction(self.menu_restore_parent_to)
        self.menubar_restore.addSeparator()
        self.menubar_restore.addAction(self.btn_restore_delete)
        self.menubar_restore.addAction(self.menu_restore_parent_delete)

        self.menubar_help = self.menubar.addMenu(_('Help'))
        self.menubar_help.addAction(self.btn_help)
        self.menubar_help.addAction(self.btn_help_config)
        self.menubar_help.addSeparator()
        self.menubar_help.addAction(self.btn_website)
        self.menubar_help.addAction(self.btn_changelog)
        self.menubar_help.addAction(self.btn_faq)
        self.menubar_help.addAction(self.btn_question)
        self.menubar_help.addAction(self.btn_bug)
        self.menubar_help.addSeparator()
        self.menubar_help.addAction(self.btn_about)

        #shortcuts without buttons
        self.shortcut_previous_folder = QShortcut(QKeySequence(Qt.ALT + Qt.Key_Left), self)
        QObject.connect(self.shortcut_previous_folder, SIGNAL('activated()'), self.on_btn_folder_history_previous_clicked)
        self.shortcut_next_folder = QShortcut(QKeySequence(Qt.ALT + Qt.Key_Right), self)
        QObject.connect(self.shortcut_next_folder, SIGNAL('activated()'), self.on_btn_folder_history_next_clicked)
        self.shortcut_open_folder = QShortcut(QKeySequence(Qt.ALT + Qt.Key_Down), self)
        QObject.connect(self.shortcut_open_folder, SIGNAL('activated()'), self.on_btn_open_current_item)

        #mouse button navigation
        self.mouseButtonEventFilter = ExtraMouseButtonEventFilter(self)
        self.setMouseButtonNavigation()

        #second spliter
        self.second_splitter = QSplitter( self )
        self.second_splitter.setOrientation( Qt.Horizontal )
        self.second_splitter.setContentsMargins(0, 0, 0, 0)
        right_layout.addWidget( self.second_splitter )

        #places
        self.list_places = QTreeWidget( self )
        self.list_places.setRootIsDecorated( False )
        self.list_places.setEditTriggers( QAbstractItemView.NoEditTriggers )
        self.list_places.setHeaderLabel( _('Shortcuts') )
        self.list_places.header().setClickable(True)
        self.list_places.header().setSortIndicatorShown(True)
        self.list_places.header().setSectionHidden(1, True)
        self.list_places.header().setSortIndicator(int(self.config.get_profile_int_value('qt4.places.SortColumn', 1)),
                                                   int(self.config.get_profile_int_value('qt4.places.SortOrder', Qt.AscendingOrder)))
        self.list_places_sort_loop = {self.config.get_current_profile(): False}
        self.second_splitter.addWidget( self.list_places )
        QObject.connect(self.list_places.header(),
                        SIGNAL('sortIndicatorChanged(int,Qt::SortOrder)'),
                        self.sort_places)

        #files view stacked layout
        widget = QWidget( self )
        self.files_view_layout = QStackedLayout( widget )
        self.second_splitter.addWidget( widget )

        #folder don't exist label
        self.lbl_folder_dont_exists = QLabel( _('This folder doesn\'t exist\nin the current selected snapshot!'), self )
        qt4tools.set_font_bold( self.lbl_folder_dont_exists )
        self.lbl_folder_dont_exists.setFrameShadow( QFrame.Sunken )
        self.lbl_folder_dont_exists.setFrameShape( QFrame.Panel )
        self.lbl_folder_dont_exists.setAlignment( Qt.AlignHCenter | Qt.AlignVCenter )
        self.files_view_layout.addWidget( self.lbl_folder_dont_exists )

        #list files view
        self.list_files_view = QTreeView( self )
        self.files_view_layout.addWidget( self.list_files_view )
        self.list_files_view.setRootIsDecorated( False )
        self.list_files_view.setAlternatingRowColors( True )
        self.list_files_view.setEditTriggers( QAbstractItemView.NoEditTriggers )
        self.list_files_view.setItemsExpandable( False )
        self.list_files_view.setDragEnabled( False )
        self.list_files_view.setSelectionMode(QAbstractItemView.ExtendedSelection)

        self.list_files_view_header = self.list_files_view.header()
        self.list_files_view_header.setClickable( True )
        self.list_files_view_header.setMovable( False )
        self.list_files_view_header.setSortIndicatorShown( True )

        self.list_files_view_model = QFileSystemModel(self)
        self.list_files_view_model.setRootPath(QDir().rootPath())
        self.list_files_view_model.setReadOnly(True)
        self.list_files_view_model.setFilter(QDir.AllDirs | QDir.AllEntries
                                            | QDir.NoDotAndDotDot | QDir.Hidden)

        self.list_files_view_proxy_model = QSortFilterProxyModel(self)
        self.list_files_view_proxy_model.setDynamicSortFilter(True)
        self.list_files_view_proxy_model.setSourceModel(self.list_files_view_model)

        self.list_files_view.setModel(self.list_files_view_proxy_model)

        self.list_files_view_delegate = QStyledItemDelegate( self )
        self.list_files_view.setItemDelegate( self.list_files_view_delegate )

        sort_column = self.config.get_int_value( 'qt4.main_window.files_view.sort.column', 0 )
        sort_order = self.config.get_bool_value( 'qt4.main_window.files_view.sort.ascending', True )
        if sort_order:
            sort_order = Qt.AscendingOrder
        else:
            sort_order = Qt.DescendingOrder

        self.list_files_view_header.setSortIndicator( sort_column, sort_order )
        self.list_files_view_model.sort(self.list_files_view_header.sortIndicatorSection(),
                                        self.list_files_view_header.sortIndicatorOrder() )
        QObject.connect(self.list_files_view_header,
                        SIGNAL('sortIndicatorChanged(int,Qt::SortOrder)'),
                        self.list_files_view_model.sort )

        self.files_view_layout.setCurrentWidget( self.list_files_view )

        #
        self.setCentralWidget( self.main_splitter )

        #context menu
        self.list_files_view.setContextMenuPolicy(Qt.CustomContextMenu)
        QObject.connect(self.list_files_view, SIGNAL('customContextMenuRequested(const QPoint&)'), self.on_context_menu)
        self.contextMenu = QMenu(self)
        self.contextMenu.addAction(self.btn_restore)
        self.contextMenu.addAction(self.btn_restore_to)
        self.contextMenu.addAction(self.btn_snapshots)
        self.contextMenu.addSeparator()
        self.btn_add_include = self.contextMenu.addAction(icon.ADD, _('Add to Include'))
        self.btn_add_exclude = self.contextMenu.addAction(icon.ADD, _('Add to Exclude'))
        QObject.connect(self.btn_add_include, SIGNAL('triggered()'), self.on_btn_add_include)
        QObject.connect(self.btn_add_exclude, SIGNAL('triggered()'), self.on_btn_add_exclude)
        self.contextMenu.addSeparator()
        self.contextMenu.addAction(self.btn_show_hidden_files)

        #ProgressBar
        self.progressBar = QProgressBar(self)
        self.progressBar.setMinimum(0)
        self.progressBar.setMaximum(100)
        self.progressBar.setValue(0)
        self.progressBar.setStyleSheet("text-align: left;")
        self.progressBar.setVisible(False)
        self.status = QLabel(self)

        self.statusBar().addWidget(self.progressBar, 100)
        self.statusBar().addWidget(self.status, 100)
        self.status.setText( _('Done') )

        self.snapshots_list = []
        self.snapshot_id = '/'
        self.path = self.config.get_profile_str_value('qt4.last_path',
                            self.config.get_str_value('qt4.last_path', '/' ) )
        self.edit_current_path.setText( self.path )
        self.path_history = tools.PathHistory(self.path)

        #restore size and position
        x = self.config.get_int_value( 'qt4.main_window.x', -1 )
        y = self.config.get_int_value( 'qt4.main_window.y', -1 )
        if x >= 0 and y >= 0:
            self.move( x, y )

        w = self.config.get_int_value( 'qt4.main_window.width', 800 )
        h = self.config.get_int_value( 'qt4.main_window.height', 500 )
        self.resize( w, h )

        main_splitter_left_w = self.config.get_int_value( 'qt4.main_window.main_splitter_left_w', 150 )
        main_splitter_right_w = self.config.get_int_value( 'qt4.main_window.main_splitter_right_w', 450 )
        sizes = [ main_splitter_left_w, main_splitter_right_w ]
        self.main_splitter.setSizes( sizes )

        second_splitter_left_w = self.config.get_int_value( 'qt4.main_window.second_splitter_left_w', 150 )
        second_splitter_right_w = self.config.get_int_value( 'qt4.main_window.second_splitter_right_w', 300 )
        sizes = [ second_splitter_left_w, second_splitter_right_w ]
        self.second_splitter.setSizes( sizes )

        files_view_name_width = self.config.get_int_value( 'qt4.main_window.files_view.name_width', -1 )
        files_view_size_width = self.config.get_int_value( 'qt4.main_window.files_view.size_width', -1 )
        files_view_date_width = self.config.get_int_value( 'qt4.main_window.files_view.date_width', -1 )
        if files_view_name_width > 0 and files_view_size_width > 0 and files_view_date_width > 0:
            self.list_files_view_header.resizeSection( 0, files_view_name_width )
            self.list_files_view_header.resizeSection( 1, files_view_size_width )
            self.list_files_view_header.resizeSection( 2, files_view_date_width )

        #force settingdialog if it is not configured
        if not config.is_configured():
            message = _('%(appName)s is not configured. Would you like '
                        'to restore a previous configuration?' % {'appName': self.config.APP_NAME})
            if QMessageBox.Yes == messagebox.warningYesNo(self, message):
                settingsdialog.RestoreConfigDialog(self).exec_()
            settingsdialog.SettingsDialog( self ).exec_()

        if not config.is_configured():
            return

        if self.snapshots.has_old_snapshots():
            settingsdialog.SettingsDialog( self ).update_snapshots_location()

        profile_id = config.get_current_profile()

        #mount
        try:
            mnt = mount.Mount(cfg = self.config, profile_id = profile_id, parent = self)
            hash_id = mnt.mount()
        except MountException as ex:
            messagebox.critical( self, str(ex) )
        else:
            self.config.set_current_hash_id(hash_id)

        if not cfg.can_backup( profile_id ):
            messagebox.critical( self, _('Can\'t find snapshots folder.\nIf it is on a removable drive please plug it and then press OK') )

        QObject.connect(self.list_files_view_proxy_model, SIGNAL('layoutChanged()'), self.on_dir_lister_completed)

        #populate lists
        self.update_profiles()
        QObject.connect( self.combo_profiles, SIGNAL('currentIndexChanged(int)'), self.on_profile_changed )

        self.list_files_view.setFocus()

        self.update_snapshot_actions()

        #signals
        QObject.connect( self.list_time_line, SIGNAL('itemSelectionChanged()'), self.on_list_time_line_current_item_changed )
        QObject.connect( self.list_places, SIGNAL('currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)'), self.on_list_places_current_item_changed )
        QObject.connect( self.list_files_view, SIGNAL('activated(const QModelIndex&)'), self.on_list_files_view_item_activated )

        self.force_wait_lock_counter = 0

        self.timer_raise_application = QTimer( self )
        self.timer_raise_application.setInterval( 1000 )
        self.timer_raise_application.setSingleShot( False )
        QObject.connect( self.timer_raise_application, SIGNAL('timeout()'), self.raise_application )
        self.timer_raise_application.start()

        self.timer_update_take_snapshot = QTimer( self )
        self.timer_update_take_snapshot.setInterval( 1000 )
        self.timer_update_take_snapshot.setSingleShot( False )
        QObject.connect( self.timer_update_take_snapshot, SIGNAL('timeout()'), self.update_take_snapshot )
        self.timer_update_take_snapshot.start()

    def closeEvent( self, event ):
        if self.shutdown.ask_before_quit():
            if QMessageBox.Yes != messagebox.warningYesNo(self, _('If you close this window Back In Time will not be able to shutdown your system when the snapshot has finished.\nDo you really want to close?') ):
                return event.ignore()

        self.config.set_str_value( 'qt4.last_path', self.path )
        self.config.set_profile_str_value('qt4.last_path', self.path)

        self.config.set_profile_int_value('qt4.places.SortColumn',
                                          self.list_places.header().sortIndicatorSection())
        self.config.set_profile_int_value('qt4.places.SortOrder',
                                          self.list_places.header().sortIndicatorOrder())

        self.config.set_int_value( 'qt4.main_window.x', self.x() )
        self.config.set_int_value( 'qt4.main_window.y', self.y() )
        self.config.set_int_value( 'qt4.main_window.width', self.width() )
        self.config.set_int_value( 'qt4.main_window.height', self.height() )

        sizes = self.main_splitter.sizes()
        self.config.set_int_value( 'qt4.main_window.main_splitter_left_w', sizes[0] )
        self.config.set_int_value( 'qt4.main_window.main_splitter_right_w', sizes[1] )

        sizes = self.second_splitter.sizes()
        self.config.set_int_value( 'qt4.main_window.second_splitter_left_w', sizes[0] )
        self.config.set_int_value( 'qt4.main_window.second_splitter_right_w', sizes[1] )

        self.config.set_int_value( 'qt4.main_window.files_view.name_width', self.list_files_view_header.sectionSize( 0 ) )
        self.config.set_int_value( 'qt4.main_window.files_view.size_width', self.list_files_view_header.sectionSize( 1 ) )
        self.config.set_int_value( 'qt4.main_window.files_view.date_width', self.list_files_view_header.sectionSize( 2 ) )

        self.config.set_bool_value( 'qt4.show_hidden_files', self.show_hidden_files )

        self.config.set_int_value( 'qt4.main_window.files_view.sort.column', self.list_files_view_header.sortIndicatorSection() )
        self.config.set_bool_value( 'qt4.main_window.files_view.sort.ascending', self.list_files_view_header.sortIndicatorOrder() == Qt.AscendingOrder )

        self.list_files_view_model.deleteLater()

        #umount
        try:
            mnt = mount.Mount(cfg = self.config, parent = self)
            mnt.umount(self.config.current_hash_id)
        except MountException as ex:
            messagebox.critical( self, str(ex) )

        self.config.save()

        event.accept()

    def update_profiles( self ):
        if self.disable_profile_changed:
            return

        self.disable_profile_changed = True

        self.combo_profiles.clear()

        index = 0
        profiles = self.config.get_profiles_sorted_by_name()

        for profile_id in profiles:
            if profile_id == self.config.get_current_profile():
                index = self.combo_profiles.count()
            self.combo_profiles.addItem( self.config.get_profile_name( profile_id ), profile_id )

        self.combo_profiles.setCurrentIndex( index )
        self.combo_profiles_action.setVisible( len( profiles ) > 1 )

        self.update_profile()

        self.disable_profile_changed = False

    def update_profile( self ):
        self.update_time_line()
        self.update_places()
        self.update_files_view( 0 )

    def on_profile_changed( self, index ):
        if self.disable_profile_changed:
            return

        profile_id = str( self.combo_profiles.itemData( index ) )
        if not profile_id:
            return
        old_profile_id = self.config.get_current_profile()
        if profile_id != old_profile_id:
            self.config.PLUGIN_MANAGER.do_unmount()
            self.remount(profile_id, old_profile_id)
            self.config.set_current_profile( profile_id )
            self.config.PLUGIN_MANAGER.load_plugins(cfg = self.config, force = True)
            self.config.PLUGIN_MANAGER.do_mount()

            self.config.set_profile_int_value('qt4.places.SortColumn',
                                              self.list_places.header().sortIndicatorSection(),
                                              old_profile_id)
            self.config.set_profile_int_value('qt4.places.SortOrder',
                                              self.list_places.header().sortIndicatorOrder(),
                                              old_profile_id)
            self.list_places_sort_loop[old_profile_id] = False
            self.list_places.header().setSortIndicator(int(self.config.get_profile_int_value('qt4.places.SortColumn', 1, profile_id)),
                                                       int(self.config.get_profile_int_value('qt4.places.SortOrder', Qt.AscendingOrder, profile_id)))

            self.config.set_profile_str_value('qt4.last_path', self.path, old_profile_id)
            path = self.config.get_profile_str_value('qt4.last_path', self.path, profile_id)
            if not path == self.path:
                self.path = path
                self.path_history.reset(self.path)
                self.edit_current_path.setText( self.path )

            self.update_profile()

    def remount( self, new_profile_id, old_profile_id):
        try:
            mnt = mount.Mount(cfg = self.config, profile_id = old_profile_id, parent = self)
            hash_id = mnt.remount(new_profile_id)
        except MountException as ex:
            messagebox.critical( self, str(ex) )
        else:
            self.config.set_current_hash_id(hash_id)

    def get_default_startup_folder_and_file( self ):
        last_path = self.config.get_str_value( 'gnome.last_path', '' )
        if last_path and os.path.isdir(last_path):
            return ( last_path, None, False )
        return ( '/', None, False )

    def get_cmd_startup_folder_and_file( self, cmd ):
        if cmd is None:
            cmd = self.app_instance.raise_cmd

        if not cmd:
            return None

        path = None
        show_snapshots = False

        for arg in cmd.split( '\n' ):
            if not arg:
                continue
            if arg == '-s' or arg == '--snapshots':
                show_snapshots = True
                continue
            if arg.startswith('-'):
                continue
            if path is None:
                path = arg

        if path is None:
            return None

        if not path:
            return None

        path = os.path.expanduser( path )
        path = os.path.abspath( path )

        if os.path.isdir( path ):
            if not path:
                show_snapshots = False

            if show_snapshots:
                return ( os.path.dirname( path ), path, True )
            else:
                return ( path, '', False )

        if os.path.isfile( path ):
            return ( os.path.dirname( path ), path, show_snapshots )

        return None

    def get_startup_folder_and_file( self, cmd = None ):
        startup_folder = self.get_cmd_startup_folder_and_file( cmd )
        if startup_folder is None:
            return self.get_default_startup_folder_and_file()
        return startup_folder

    def raise_application( self ):
        raise_cmd = self.app_instance.raise_command()
        if raise_cmd is None:
            return

        logger.debug("Raise cmd: %s" %raise_cmd, self)
        self.qapp.alert( self )

    def update_take_snapshot( self, force_wait_lock = False ):
        if force_wait_lock:
            self.force_wait_lock_counter = 10

        busy = self.snapshots.is_busy()
        if busy:
            self.force_wait_lock_counter = 0

        if self.force_wait_lock_counter > 0:
            self.force_wait_lock_counter = self.force_wait_lock_counter - 1

        fake_busy = busy or self.force_wait_lock_counter > 0

        message = _('Working:')
        take_snapshot_message = self.snapshots.get_take_snapshot_message()
        if fake_busy:
            if take_snapshot_message is None:
                take_snapshot_message = ( 0, '...' )
        elif take_snapshot_message is None:
            take_snapshot_message = self.last_take_snapshot_message
            if take_snapshot_message is None:
                take_snapshot_message = ( 0, _('Done') )

        force_update = False

        if fake_busy:
            if self.btn_take_snapshot.isEnabled():
                self.btn_take_snapshot.setEnabled( False )
        elif not self.btn_take_snapshot.isEnabled():
            force_update = True

            self.btn_take_snapshot.setEnabled( True )

            snapshots_list = self.snapshots.get_snapshots_and_other_list()

            if snapshots_list != self.snapshots_list:
                self.snapshots_list = snapshots_list
                self.update_time_line( False )
                take_snapshot_message = ( 0, _('Done') )
            else:
                if take_snapshot_message[0] == 0:
                    take_snapshot_message = ( 0, _('Done, no backup needed') )

            self.shutdown.shutdown()

        if take_snapshot_message != self.last_take_snapshot_message or force_update:
            self.last_take_snapshot_message = take_snapshot_message

            if fake_busy:
                message = _('Working:') + ' ' + self.last_take_snapshot_message[1].replace( '\n', ' ' )
            elif take_snapshot_message[0] == 0:
                message = self.last_take_snapshot_message[1].replace( '\n', ' ' )
            else:
                message = _('Error:') + ' ' + self.last_take_snapshot_message[1].replace( '\n', ' ' )

            self.status.setText(message)

        pg = progress.ProgressFile(self.config)
        if pg.isFileReadable():
            self.progressBar.setVisible(True)
            self.status.setVisible(False)
            pg.load()
            self.progressBar.setValue(pg.get_int_value('percent') )
            self.progressBar.setFormat(' | '.join(self.getProgressBarFormat(pg, self.status.text())) )
        else:
            self.progressBar.setVisible(False)
            self.status.setVisible(True)

        #if not fake_busy:
        #	self.last_take_snapshot_message = None

    def getProgressBarFormat(self, pg, message):
        d = (('sent',   _('Sent:')), \
             ('speed',  _('Speed:')),\
             ('eta',    _('ETA:')) )
        yield ' %p%'
        for key, txt in d:
            value = pg.get_str_value(key, '')
            if not value:
                continue
            yield txt + ' ' + value
        yield message

    def on_list_places_current_item_changed( self, item, previous ):
        if item is None:
            return

        path = str( item.data( 0, Qt.UserRole ) )
        if not path:
            return

        if path == self.path:
            return

        self.path = path
        self.path_history.append(path)
        self.update_files_view( 3 )

    def add_place( self, name, path, icon ):
        item = QTreeWidgetItem()

        item.setText( 0, name )

        if icon:
            item.setIcon( 0, QIcon.fromTheme( icon ) )

        item.setData( 0, Qt.UserRole, path )

        if not path:
            item.setFont( 0, qt4tools.get_font_bold( item.font( 0 ) ) )
            item.setFlags( Qt.ItemIsEnabled )
            item.setBackgroundColor( 0, QColor( 196, 196, 196 ) )
            item.setTextColor( 0, QColor( 60, 60, 60 ))

        self.list_places.addTopLevelItem( item )

        if path == self.path:
            self.list_places.setCurrentItem( item )

        return item

    def update_places( self ):
        self.list_places.clear()
        self.add_place( _('Global'), '', '' )
        self.add_place( _('Root'), '/', 'computer' )
        self.add_place( _('Home'), os.path.expanduser( '~' ), 'user-home' )

        #add backup folders
        include_folders = self.config.get_include()
        if include_folders:
            folders = []
            for item in include_folders:
                if item[1] == 0:
                    folders.append( item[0] )

            if folders:
                sortColumn = self.list_places.header().sortIndicatorSection()
                sortOrder  = self.list_places.header().sortIndicatorOrder()
                if not sortColumn:
                    folders.sort(key = lambda v: (v.upper(), v[0].islower()), reverse = sortOrder)
                self.add_place( _('Backup folders'), '', '' )
                for folder in folders:
                    self.add_place( folder, folder, 'document-save' )

    def sort_places(self, newColumn, newOrder, force = False):
        profile_id = self.config.get_current_profile()
        if newColumn == 0 and newOrder == Qt.AscendingOrder:
            if profile_id in self.list_places_sort_loop and self.list_places_sort_loop[profile_id]:
                newColumn, newOrder = 1, Qt.AscendingOrder
                self.list_places.header().setSortIndicator(newColumn, newOrder)
                self.list_places_sort_loop[profile_id] = False
            else:
                self.list_places_sort_loop[profile_id] = True
        self.update_places()

    def update_snapshot_actions( self, item = None ):
        enabled = False

        if item is None:
            item = self.list_time_line.currentItem()
        if not item is None:
            if len(item.snapshotID()) > 1:
                enabled = True

        #update remove/name snapshot buttons
        self.btn_name_snapshot.setEnabled( enabled )
        self.btn_remove_snapshot.setEnabled( enabled )
        self.btn_snapshot_log_view.setEnabled( enabled )

    def on_list_time_line_current_item_changed(self):
        item = self.list_time_line.currentItem()
        self.update_snapshot_actions(item)

        if item is None:
            return

        snapshot_id = item.snapshotID()
        if not snapshot_id:
            return

        if snapshot_id == self.snapshot_id:
            return

        self.snapshot_id = snapshot_id
        self.update_files_view( 2 )

    def time_line_update_snapshot_name( self, item ):
        snapshot_id = item.snapshotID()
        if snapshot_id:
            item.setText( 0, self.snapshots.get_snapshot_display_name( snapshot_id ) )

    def update_time_line( self, get_snapshots_list = True ):
        self.list_time_line.clear()
        self.list_time_line.addRoot('/',
                                    self.snapshots.get_snapshot_display_name('/'),
                                    _('This is NOT a snapshot but a live view of your local files'))
        if get_snapshots_list:
            self.snapshots_list = []
            thread = FillTimeLineThread(self)
            thread.addSnapshot.connect(self.list_time_line.addSnapshot)
            thread.finished.connect(self.list_time_line.checkSelection)
            thread.start()
        else:
            for sid in self.snapshots_list:
                item = self.list_time_line.addSnapshot(sid,
                                                       self.snapshots.get_snapshot_display_name(sid),
                                                       _('Last check %s') %self.snapshots.get_snapshot_last_check(sid))
            self.list_time_line.checkSelection()

    def time_line_set_current_snapshot(self, new_snapshot_id):
        for item in self.list_time_line.iterSnapshotItems():
            if item.snapshotID() == new_snapshot_id:
                self.snapshot_id = new_snapshot_id
                self.list_time_line.setCurrentItem( item )
                self.update_files_view( 2 )
                break

    def on_btn_take_snapshot_clicked( self ):
        backintime.take_snapshot_now_async( self.config )
        self.update_take_snapshot( True )

    def on_btn_update_snapshots_clicked( self ):
        self.update_time_line()
        self.update_files_view( 2 )

    def on_btn_name_snapshot_clicked( self ):
        item = self.list_time_line.currentItem()
        if item is None:
            return

        snapshot_id = item.snapshotID()
        if len( snapshot_id ) <= 1:
            return

        name = self.snapshots.get_snapshot_name( snapshot_id )

        ret_val = QInputDialog.getText(self, _('Snapshot Name'), str() )
        if not ret_val[1]:
            return

        new_name = ret_val[0].strip()
        if name == new_name:
            return

        self.snapshots.set_snapshot_name( snapshot_id, new_name )
        self.time_line_update_snapshot_name( item )

    def on_btn_log_view_clicked ( self ):
        self.removeMouseButtonNavigation()
        logviewdialog.LogViewDialog( self ).exec_()
        self.setMouseButtonNavigation()

    def on_btn_snapshot_log_view_clicked ( self ):
        item = self.list_time_line.currentItem()
        if item is None:
            return

        snapshot_id = item.snapshotID()
        if len( snapshot_id ) <= 1:
            return

        self.removeMouseButtonNavigation()
        dlg = logviewdialog.LogViewDialog( self, snapshot_id )
        dlg.exec_()
        if snapshot_id != dlg.snapshot_id:
            self.time_line_set_current_snapshot(dlg.snapshot_id)
        self.setMouseButtonNavigation()

    def on_btn_remove_snapshot_clicked ( self ):
        items = [item for item in self.list_time_line.selectedItems() if len(item.snapshotID()) > 1]
        if not items:
            return

        if QMessageBox.Yes != messagebox.warningYesNo( self, \
                              _('Are you sure you want to remove the snapshot:\n%s') \
                                %'\n'.join([self.snapshots.get_snapshot_display_name(item.snapshotID()) \
                                            for item in items]) ):
            return

        for item in items:
            item.setDisabled(True)
        thread = RemoveSnapshotThread(self, items)
        thread.refreshSnapshotList.connect(self.update_time_line)
        thread.start()

    def on_btn_settings_clicked( self ):
        self.removeMouseButtonNavigation()
        if QDialog.Accepted == settingsdialog.SettingsDialog( self ).exec_():
            profile_id = self.config.get_current_profile()
            self.remount(profile_id, profile_id)
            self.update_profiles()
        self.setMouseButtonNavigation()

    def on_btn_shutdown_toggled(self, checked):
        self.shutdown.activate_shutdown = checked

    def on_context_menu(self, point):
        self.contextMenu.exec_(self.list_files_view.mapToGlobal(point) )

    def on_about( self ):
        self.removeMouseButtonNavigation()
        dlg = About(self)
        dlg.exec_()
        self.setMouseButtonNavigation()

    def on_help( self ):
        self.open_man_page('backintime')

    def on_help_config(self):
        self.open_man_page('backintime-config')

    def on_website( self ):
        self.open_url('https://github.com/bit-team/backintime')

    def on_changelog( self ):
        def a_href(m):
            if m.group(0).count('@'):
                return '<a href="mailto:%(url)s">%(url)s</a>' % {'url': m.group(0)}
            else:
                return '<a href="%(url)s">%(url)s</a>' % {'url': m.group(0)}

        def a_href_lp(m):
            return '<a href="https://bugs.launchpad.net/backintime/+bug/%(id)s">%(txt)s</a>' % {'txt': m.group(0), 'id': m.group(1)}

        msg = self.config.get_changelog()
        msg = re.sub(r'https?://[^) \n]*', a_href, msg)
        msg = re.sub(r'(?:LP:|bug) ?#?(\d+)', a_href_lp, msg)
        msg = re.sub(r'\n', '<br>', msg)
        messagebox.show_info(self, _('Changelog'), msg)

    def on_faq( self ):
        self.open_url('https://github.com/bit-team/backintime/wiki/FAQ')

    def on_ask_a_question( self ):
        self.open_url('https://github.com/bit-team/backintime/issues')

    def on_report_a_bug( self ):
        self.open_url('https://github.com/bit-team/backintime/issues/new')

    def open_url( self, url ):
        return QDesktopServices.openUrl(QUrl(url))

    def open_man_page(self, man_page):
        if not tools.check_command('man'):
            messagebox.critical(self, "Couldn't find 'man' to show the help page. Please install 'man'")
            return
        env = os.environ
        env['MANWIDTH'] = '80'
        proc = subprocess.Popen(['man', man_page],
                                stdout = subprocess.PIPE,
                                universal_newlines = True,
                                env = env)
        out, err = proc.communicate()
        messagebox.show_info(self, 'Manual Page {}'.format(man_page), out)

    def on_btn_show_hidden_files_toggled( self, checked ):
        self.show_hidden_files = checked
        self.update_files_view( 1 )

    def backup_on_restore(self):
        cb = QCheckBox(_("Backup local files before overwriting or\nremoving with trailing '%(suffix)s'.")
                       % {'suffix': self.snapshots.backup_suffix()} )
        cb.setChecked(self.config.is_backup_on_restore_enabled())
        return {'widget': cb, 'retFunc': cb.isChecked, 'id': 'backup'}

    def confirm_delete_on_restore(self, paths, warn_root = False):
        msg = _('Are you sure you want to remove all newer files in your '
                'original folder?')
        if warn_root:
            msg += '\n\n'
            msg += _('WARNING: deleting files in filesystem root could break your whole system!!!')
        msg += '\n\n'
        msg += _('Files to be restored:')
        msg += '\n'
        msg += '\n'.join(paths)

        confirm, opt = messagebox.warningYesNoOptions(self, msg, (self.backup_on_restore(), ) )
        ret = {'backup': False, 'no_backup': False}
        if self.config.is_backup_on_restore_enabled():
            if not opt['backup']:
                ret['no_backup'] = True
        else:
            if opt['backup']:
                ret['backup'] = True
        return (confirm, ret)

    def confirm_restore(self, paths):
        msg = _('Do you really want to restore this files(s):')
        msg += '\n'
        msg += '\n'.join(paths)
        confirm, opt = messagebox.warningYesNoOptions(self, msg, (self.backup_on_restore(), ) )
        ret = {'backup': False, 'no_backup': False}
        if self.config.is_backup_on_restore_enabled():
            if not opt['backup']:
                ret['no_backup'] = True
        else:
            if opt['backup']:
                ret['backup'] = True
        return (confirm, ret)

    def restore_this( self, delete = False ):
        if len( self.snapshot_id ) <= 1:
            return

        selected_file = [f for f, idx in self.multi_file_selected()]
        if not selected_file:
            return
        rel_path = [os.path.join(self.path, x) for x in selected_file]

        self.removeMouseButtonNavigation()
        if delete:
            confirm, kwargs = self.confirm_delete_on_restore(rel_path, any([i == '/' for i in selected_file]) )
        else:
            confirm, kwargs = self.confirm_restore(rel_path)
        self.setMouseButtonNavigation()
        if not confirm:
            return

        restoredialog.restore(self, self.snapshot_id, rel_path, delete = delete, **kwargs)

    def restore_this_to( self ):
        if len( self.snapshot_id ) <= 1:
            return

        selected_file = [f for f, idx in self.multi_file_selected()]
        if not selected_file:
            return
        rel_path = [os.path.join(self.path, x) for x in selected_file]

        confirm, kwargs = self.confirm_restore(rel_path)
        if not confirm:
            return

        restoredialog.restore(self, self.snapshot_id, rel_path, None, **kwargs)

    def restore_parent( self, delete = False ):
        if len( self.snapshot_id ) <= 1:
            return

        self.removeMouseButtonNavigation()
        if delete:
            confirm, kwargs = self.confirm_delete_on_restore((self.path,), self.path == '/')
        else:
            confirm, kwargs = self.confirm_restore((self.path,))
        self.setMouseButtonNavigation()
        if not confirm:
            return

        restoredialog.restore( self, self.snapshot_id, self.path, delete = delete, **kwargs)

    def restore_parent_to( self ):
        if len( self.snapshot_id ) <= 1:
            return

        if not self.confirm_restore((self.path,)):
            return

        restoredialog.restore( self, self.snapshot_id, self.path, None )

    def on_btn_snapshots_clicked( self ):
        selected_file, idx = self.file_selected()
        if not selected_file:
            return

        rel_path = os.path.join( self.path, selected_file )

        self.removeMouseButtonNavigation()
        dlg = snapshotsdialog.SnapshotsDialog( self, self.snapshot_id, rel_path)
        if QDialog.Accepted == dlg.exec_():
            if dlg.snapshot_id != self.snapshot_id:
                self.time_line_set_current_snapshot(dlg.snapshot_id)
        self.setMouseButtonNavigation()

    def on_btn_folder_up_clicked( self ):
        if len( self.path ) <= 1:
            return

        path = os.path.dirname( self.path )
        if self.path == path:
            return

        self.path = path
        self.path_history.append(self.path)
        self.update_files_view( 0 )

    def on_btn_folder_history_previous_clicked(self):
        path = self.path_history.previous()
        full_path = self.snapshots.get_snapshot_path_to(self.snapshot_id, path)
        if os.path.isdir(full_path):
            if self.snapshots.can_open_path(self.snapshot_id, full_path):
                self.path = path
                self.update_files_view(0)

    def on_btn_folder_history_next_clicked(self):
        path = self.path_history.next()
        full_path = self.snapshots.get_snapshot_path_to(self.snapshot_id, path)
        if os.path.isdir(full_path):
            if self.snapshots.can_open_path(self.snapshot_id, full_path):
                self.path = path
                self.update_files_view(0)

    def on_btn_open_current_item(self):
        path, idx = self.file_selected()
        if not path:
            return
        self.open_path(path)

    def on_btn_add_include(self):
        selected_file = [f for f, idx in self.multi_file_selected()]
        if not selected_file:
            return
        rel_path = [os.path.join(self.path, x) for x in selected_file]
        include = self.config.get_include()
        update_places = False
        for item in rel_path:
            if os.path.isdir(item):
                include.append((item, 0))
                update_places = True
            else:
                include.append((item, 1))
        self.config.set_include(include)
        if update_places:
            self.update_places()

    def on_btn_add_exclude(self):
        selected_file = [f for f, idx in self.multi_file_selected()]
        if not selected_file:
            return
        rel_path = [os.path.join(self.path, x) for x in selected_file]
        exclude = self.config.get_exclude()
        exclude.extend(rel_path)
        self.config.set_exclude(exclude)

    def on_list_files_view_item_activated( self, model_index ):
        if self.qapp.keyboardModifiers() and Qt.ControlModifier:
            return

        if model_index is None:
            return

        rel_path = str( self.list_files_view_proxy_model.data( model_index ) )
        if not rel_path:
            return
        self.open_path(rel_path)

    def open_path(self, rel_path):
        rel_path = os.path.join( self.path, rel_path )
        full_path = self.snapshots.get_snapshot_path_to( self.snapshot_id, rel_path )

        if os.path.exists( full_path ):
            if self.snapshots.can_open_path( self.snapshot_id, full_path ):
                if os.path.isdir( full_path ):
                    self.path = rel_path
                    self.path_history.append(rel_path)
                    self.update_files_view( 0 )
                else:
                    self.run = QDesktopServices.openUrl(QUrl(full_path ))

    def files_view_get_name( self, item ):
        return item.text( 0 )

    def files_view_get_type( self, item ):
        return int( item.text( 4 ) )

    @pyqtSlot(int)
    def update_files_view( self, changed_from, selected_file = None, show_snapshots = False ): #0 - files view change directory, 1 - files view, 2 - time_line, 3 - places
        if 0 == changed_from or 3 == changed_from:
            selected_file = ''

        if 0 == changed_from:
            #update places
            self.list_places.setCurrentItem( None )
            for place_index in range( self.list_places.topLevelItemCount() ):
                item = self.list_places.topLevelItem( place_index )
                if self.path == str( item.data( 0, Qt.UserRole ) ):
                    self.list_places.setCurrentItem( item )
                    break

        tooltip = ''
        text = ''
        if len( self.snapshot_id ) > 1:
            name = self.snapshots.get_snapshot_display_id( self.snapshot_id )
            text = _('Snapshot: %s') % name
            tooltip = _('View the snapshot made at %s') % name
        else:
            tooltip = _('View the current disk content')
            text = _('Now')

        self.right_widget.setTitle( _( text ) )
        self.right_widget.setToolTip( _( tooltip ) )

        #try to keep old selected file
        if selected_file is None:
            selected_file, idx = self.file_selected()

        self.selected_file = selected_file

        #update files view
        full_path = self.snapshots.get_snapshot_path_to( self.snapshot_id, self.path )

        if os.path.isdir( full_path ):
            if self.show_hidden_files:
                self.list_files_view_proxy_model.setFilterRegExp(r'')
            else:
                self.list_files_view_proxy_model.setFilterRegExp(r'^[^\.]')

            model_index = self.list_files_view_model.index(full_path)
            proxy_model_index = self.list_files_view_proxy_model.mapFromSource(model_index)
            self.list_files_view.setRootIndex(proxy_model_index)

            self.files_view_toolbar.setEnabled( False )
            self.files_view_layout.setCurrentWidget( self.list_files_view )
            #todo: find a signal for this
            self.on_dir_lister_completed()
        else:
            self.btn_restore_menu.setEnabled( False )
            self.menubar_restore.setEnabled(False)
            self.btn_restore.setEnabled(False)
            self.btn_restore_to.setEnabled(False)
            self.btn_snapshots.setEnabled( False )
            self.files_view_layout.setCurrentWidget( self.lbl_folder_dont_exists )

        #show current path
        self.edit_current_path.setText( self.path )
        self.menu_restore_parent.setText( _("Restore '%s'") % self.path )
        self.menu_restore_parent_to.setText( _("Restore '%s' to ...") % self.path )
        self.menu_restore_parent_delete.setText( _("Restore '%s' and delete new files") % self.path )

        #update folder_up button state
        self.btn_folder_up.setEnabled( len( self.path ) > 1 )

    def on_dir_lister_completed( self ):
        has_files = (self.list_files_view_proxy_model.rowCount(self.list_files_view.rootIndex() ) > 0 )

        #update restore button state
        enable = len(self.snapshot_id) > 1 and has_files
        self.btn_restore_menu.setEnabled(enable)
        self.menubar_restore.setEnabled(enable)
        self.btn_restore.setEnabled(enable)
        self.btn_restore_to.setEnabled(enable)

        #update snapshots button state
        self.btn_snapshots.setEnabled( has_files )

        #enable files toolbar
        self.files_view_toolbar.setEnabled( True )

        #select selected_file
        found = False

        if self.selected_file:
            index = self.list_files_view.indexAt(QPoint(0,0))
            if not index.isValid():
                return
            while index.isValid():
                file_name = (str( self.list_files_view_proxy_model.data(index) ))
                if file_name == self.selected_file:
                    self.list_files_view.setCurrentIndex(index)
                    found = True
                    break
                index = self.list_files_view.indexBelow(index)
            self.selected_file = ''

        if not found and has_files:
            self.list_files_view.setCurrentIndex( self.list_files_view_proxy_model.index( 0, 0 ) )

    def file_selected(self):
        idx = self.list_files_view.currentIndex()
        idx = self.index_first_column(idx)
        selected_file = str( self.list_files_view_proxy_model.data( idx ) )
        if selected_file == '/':
            #nothing is selected
            return(None, None)
        return(selected_file, idx)

    def multi_file_selected(self):
        for idx in self.list_files_view.selectedIndexes():
            if idx.column() > 0:
                continue
            selected_file = str(self.list_files_view_proxy_model.data(idx))
            yield (selected_file, idx)

    def index_first_column(self, idx):
        if idx.column() > 0:
            idx = idx.sibling(idx.row(), 0)
        return idx

    def setMouseButtonNavigation(self):
        self.qapp.installEventFilter(self.mouseButtonEventFilter)

    def removeMouseButtonNavigation(self):
        self.qapp.removeEventFilter(self.mouseButtonEventFilter)

class About(QDialog):
    def __init__(self, parent = None):
        super(About, self).__init__(parent)
        self.parent = parent
        self.config = parent.config
        import icon

        self.setWindowTitle(_('About') + ' ' + self.config.APP_NAME)
        logo     = QLabel('Icon')
        logo.setPixmap(icon.BIT_LOGO.pixmap(QSize(48, 48)) )
        version = self.config.VERSION
        ref, hashid = tools.get_git_ref_hash()
        git_version = ''
        if ref:
            git_version = " git branch '{}' hash '{}'".format(ref, hashid)
        name = QLabel('<h1>' + self.config.APP_NAME + ' ' + version + '</h1>' + git_version)
        name.setAlignment(Qt.AlignLeft | Qt.AlignTop)
        homepage = QLabel(self.mkurl('<https://github.com/bit-team/backintime>'))
        homepage.setTextInteractionFlags(Qt.LinksAccessibleByMouse)
        homepage.setOpenExternalLinks(True)
        bit_copyright = QLabel(self.config.COPYRIGHT + '\n')

        vlayout = QVBoxLayout(self)
        hlayout = QHBoxLayout()
        hlayout.addWidget(logo)
        hlayout.addWidget(name)
        hlayout.addStretch()
        vlayout.addLayout(hlayout)
        vlayout.addWidget(homepage)
        vlayout.addWidget(bit_copyright)

        button_box_left  = QDialogButtonBox(self)
        btn_authors      = button_box_left.addButton(_('Authors'), QDialogButtonBox.ActionRole)
        btn_translations = button_box_left.addButton(_('Translations'), QDialogButtonBox.ActionRole)
        btn_license      = button_box_left.addButton(_('License'), QDialogButtonBox.ActionRole)

        button_box_right = QDialogButtonBox(QDialogButtonBox.Ok)

        hlayout = QHBoxLayout()
        hlayout.addWidget(button_box_left)
        hlayout.addWidget(button_box_right)
        vlayout.addLayout(hlayout)

        QObject.connect(btn_authors, SIGNAL('clicked()'), self.authors)
        QObject.connect(btn_translations, SIGNAL('clicked()'), self.translations)
        QObject.connect(btn_license, SIGNAL('clicked()'), self.license)
        QObject.connect(button_box_right, SIGNAL('accepted()'), self.accept)

    def authors(self):
        return messagebox.show_info(self, _('Authors'), self.mkurl(self.config.get_authors()) )

    def translations(self):
        return messagebox.show_info(self, _('Translations'), self.mkurl(self.config.get_translations()) )

    def license(self):
        return messagebox.show_info(self, _('License'), self.config.get_license())

    def mkurl(self, msg):
        msg = re.sub(r'<(.*?)>', self.a_href, msg)
        msg = re.sub(r'\n', '<br>', msg)
        return msg

    def a_href(self, m):
        if m.group(1).count('@'):
            return '<a href="mailto:%(url)s">%(url)s</a>' % {'url': m.group(1)}
        else:
            return '<a href="%(url)s">%(url)s</a>' % {'url': m.group(1)}

class ExtraMouseButtonEventFilter(QObject):
    '''globally catch mouse buttons 4 and 5 (mostly used as back and forward)
    and assign it to browse in file history.
    When updating to Qt5 use Qt.BackButton and Qt.ForwardButton instead.
    '''
    def __init__(self, main_window):
        self.main_window = main_window
        super(ExtraMouseButtonEventFilter, self).__init__()

    def eventFilter(self, receiver, event):
        if event.type() == QEvent.MouseButtonPress and event.button() in (Qt.XButton1, Qt.XButton2):
            if event.button() == Qt.XButton1:
                self.main_window.on_btn_folder_history_previous_clicked()
            if event.button() == Qt.XButton2:
                self.main_window.on_btn_folder_history_next_clicked()
            return True
        else:
            return super(ExtraMouseButtonEventFilter, self).eventFilter(receiver, event)

class RemoveSnapshotThread(QThread):
    '''remove snapshots in background thread so GUI will not freeze
    '''
    refreshSnapshotList = pyqtSignal()
    def __init__(self, parent, items):
        self.config = parent.config
        self.snapshots = parent.snapshots
        self.items = items
        super(RemoveSnapshotThread, self).__init__(parent)

    def run(self):
        last_snapshot = self.snapshots.get_snapshots_list()[0]
        renew_last_snapshot = False
        #inhibit suspend/hibernate during delete
        self.config.inhibitCookie = tools.inhibitSuspend(toplevel_xid = self.config.xWindowId,
                                                         reason = 'deleting snapshots')

        for item in self.items:
            self.snapshots.remove_snapshot(item.snapshotID())
            try:
                item.setHidden(True)
            except RuntimeError:
                #item has been deleted
                #probably because user pressed refresh
                pass
            if item.snapshotID() == last_snapshot:
                renew_last_snapshot = True

        tools.update_cached_fs(self.config.get_snapshots_full_path())
        self.refreshSnapshotList.emit()

        #set correct last snapshot again
        sids = self.snapshots.get_snapshots_list()
        if renew_last_snapshot and len(sids):
            self.snapshots.create_last_snapshot_symlink(sids[0])

        #release inhibit suspend
        if self.config.inhibitCookie:
            self.config.inhibitCookie = tools.unInhibitSuspend(*self.config.inhibitCookie)

class FillTimeLineThread(QThread):
    '''add snapshot IDs to timeline in background
    '''
    addSnapshot = pyqtSignal(str, str, str)
    def __init__(self, parent):
        self.parent = parent
        self.snapshots = parent.snapshots
        super(FillTimeLineThread, self).__init__(parent)

    def run(self):
        for sid in self.snapshots.get_snapshots_and_other():
            self.addSnapshot.emit(sid,
                                  self.snapshots.get_snapshot_display_name(sid),
                                  _('Last check %s') %self.snapshots.get_snapshot_last_check(sid))
            self.parent.snapshots_list.append(sid)

        self.parent.snapshots_list.sort()

def debug_trace():
    '''Set a tracepoint in the Python debugger that works with Qt'''
    from pdb import set_trace
    pyqtRemoveInputHook()
    set_trace()

if __name__ == '__main__':
    cfg = backintime.start_app( 'backintime-qt4' )

    raise_cmd = ''
    if len( sys.argv ) > 1:
        raise_cmd = '\n'.join( sys.argv[ 1 : ] )

    app_instance = guiapplicationinstance.GUIApplicationInstance( cfg.get_app_instance_file(), raise_cmd )
    cfg.PLUGIN_MANAGER.load_plugins(cfg = cfg)
    cfg.PLUGIN_MANAGER.on_app_start()

    logger.openlog()
    qapp = qt4tools.create_qapplication(cfg.APP_NAME)

    main_window = MainWindow( cfg, app_instance, qapp )

    if cfg.is_configured():
        cfg.xWindowId = main_window.winId()
        main_window.show()
        qapp.exec_()

    logger.closelog()

    cfg.PLUGIN_MANAGER.on_app_exit()
    app_instance.exit_application()