File: filehandling.py

package info (click to toggle)
mypaint 2.0.1-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 27,760 kB
  • sloc: python: 43,902; cpp: 6,920; xml: 2,475; sh: 473; makefile: 26
file content (1576 lines) | stat: -rw-r--r-- 57,168 bytes parent folder | download | duplicates (3)
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
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
# -*- coding: utf-8 -*-
# This file is part of MyPaint.
# Copyright (C) 2009-2019 by the MyPaint Development Team
# Copyright (C) 2007-2014 by Martin Renold <martinxyz@gmx.ch>
#
# 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.

"""File opening/saving."""


## Imports

from __future__ import division, print_function

import os
import re
from glob import glob
import sys
import logging
from collections import OrderedDict
import time

from lib.gibindings import Gtk
from lib.gibindings import Pango

from lib import helpers
from lib import fileutils
from lib.errors import FileHandlingError
from lib.errors import AllocationError
import gui.compatibility as compat
from gui.widgets import with_wait_cursor
from lib import mypaintlib
from lib.gettext import ngettext
from lib.gettext import C_
import lib.glib
from lib.glib import filename_to_unicode
import lib.xml
import lib.feedback
from lib.pycompat import unicode, PY3

logger = logging.getLogger(__name__)


## Save format consts

class _SaveFormat:
    """Safe format consts."""
    ANY = 0
    ORA = 1
    PNG_AUTO = 2
    PNG_SOLID = 3
    PNG_TRANS = 4
    PNGS_BY_LAYER = 5
    PNGS_BY_VIEW = 6
    JPEG = 7


## Internal helper funcs


def _get_case_insensitive_glob(string):
    """Converts a glob pattern into a case-insensitive glob pattern.

    >>> _get_case_insensitive_glob('*.ora')
    '*.[oO][rR][aA]'

    This utility function is a workaround for the GTK
    FileChooser/FileFilter not having an easy way to use case
    insensitive filters

    """
    ext = string.split('.')[1]
    globlist = ["[%s%s]" % (c.lower(), c.upper()) for c in ext]
    return '*.%s' % ''.join(globlist)


def _add_filters_to_dialog(filters, dialog):
    """Adds Gtk.FileFilter objs for patterns to a dialog."""
    for name, patterns in filters:
        f = Gtk.FileFilter()
        f.set_name(name)
        for p in patterns:
            f.add_pattern(_get_case_insensitive_glob(p))
        dialog.add_filter(f)


def _dialog_set_filename(dialog, s):
    """Sets the filename and folder visible in a dialog.

    According to the PyGTK documentation we should use set_filename();
    however, doing so removes the selected file filter.

    TODO: verify whether this is still needed with GTK3+PyGI.

    """
    path, name = os.path.split(s)
    dialog.set_current_folder(path)
    dialog.set_current_name(name)


## Class definitions

class _IOProgressUI:
    """Wraps IO activity calls to show progress to the user.

    Code about to do a potentially lengthy save or load operation
    constructs one one of these temporary state manager objects, and
    uses it to call their supplied IO callable.  The _IOProgressUI
    supplies the IO callable with a lib.feedback.Progress object which
    deeper levels will need to call regularly to keep the UI updated.
    Statusbar messages and error or progress dialogs may be shown via
    the main application.

    Yes, this sounds a lot like context managers and IO coroutines,
    and maybe one day it all will be just that.

    """

    # Message templating consts:

    _OP_DURATION_TEMPLATES = {
        "load": C_(
            "Document I/O: message shown while working",
            u"Loading {files_summary}…",
        ),
        "import": C_(
            "Document I/O: message shown while working",
            u"Importing layers from {files_summary}…",
        ),
        "save": C_(
            "Document I/O: message shown while working",
            u"Saving {files_summary}…",
        ),
        "export": C_(
            "Document I/O: message shown while working",
            u"Exporting to {files_summary}…",
        ),
    }

    _OP_FAILED_TEMPLATES = {
        "export": C_(
            "Document I/O: fail message",
            u"Failed to export to {files_summary}.",
        ),
        "save": C_(
            "Document I/O: fail message",
            u"Failed to save {files_summary}.",
        ),
        "import": C_(
            "Document I/O: fail message",
            u"Could not import layers from {files_summary}.",
        ),
        "load": C_(
            "Document I/O: fail message",
            u"Could not load {files_summary}.",
        ),
    }

    _OP_FAIL_DIALOG_TITLES = {
        "save": C_(
            "Document I/O: fail dialog title",
            u"Save failed",
        ),
        "export": C_(
            "Document I/O: fail dialog title",
            u"Export failed",
        ),
        "import": C_(
            "Document I/O: fail dialog title",
            u"Import Layers failed",
        ),
        "load": C_(
            "Document I/O: fail dialog title",
            u"Open failed",
        ),
    }

    _OP_SUCCEEDED_TEMPLATES = {
        "export": C_(
            "Document I/O: success",
            u"Exported to {files_summary} successfully.",
        ),
        "save": C_(
            "Document I/O: success",
            u"Saved {files_summary} successfully.",
        ),
        "import": C_(
            "Document I/O: success",
            u"Imported layers from {files_summary}.",
        ),
        "load": C_(
            "Document I/O: success",
            u"Loaded {files_summary}.",
        ),
    }

    # Message templating:

    @staticmethod
    def format_files_summary(f):
        """The suggested way of formatting 1+ filenames for display.

        :param f: A list of filenames, or a single filename.
        :returns: A files_summary value for the constructor.
        :rtype: unicode|str

        """
        if isinstance(f, tuple) or isinstance(f, list):
            nfiles = len(f)
            # TRANSLATORS: formatting for {files_summary} for multiple files.
            # TRANSLATORS: corresponding msgid for single files: "“{basename}”"
            return ngettext(u"{n} file", u"{n} files", nfiles).format(
                n=nfiles,
            )
        elif isinstance(f, bytes) or isinstance(f, unicode):
            if isinstance(f, bytes):
                f = f.decode("utf-8")
            return C_(
                "Document I/O: the {files_summary} for a single file",
                u"“{basename}”",
            ).format(basename=os.path.basename(f))
        else:
            raise TypeError("Expected a string, or a sequence of strings.")

    # Method defs:

    def __init__(self, app, op_type, files_summary,
                 use_statusbar=True, use_dialogs=True):
        """Construct, describing what UI messages to show.

        :param app: The top-level MyPaint application object.
        :param str op_type: What kind of operation is about to happen.
        :param unicode files-summary: User-visible descripion of files.
        :param bool use_statusbar: Show statusbar messages for feedback.
        :param bool use_dialogs: Whether to use dialogs for feedback.

        """
        self._app = app
        self.clock_func = time.perf_counter if PY3 else time.clock

        files_summary = unicode(files_summary)
        op_type = str(op_type)
        if op_type not in self._OP_DURATION_TEMPLATES:
            raise ValueError("Unknown operation type %r" % (op_type,))

        msg = self._OP_DURATION_TEMPLATES[op_type].format(
            files_summary = files_summary,
        )
        self._duration_msg = msg

        msg = self._OP_SUCCEEDED_TEMPLATES[op_type].format(
            files_summary = files_summary,
        )
        self._success_msg = msg

        msg = self._OP_FAILED_TEMPLATES[op_type].format(
            files_summary = files_summary,
        )
        self._fail_msg = msg

        msg = self._OP_FAIL_DIALOG_TITLES[op_type]
        self._fail_dialog_title = msg

        self._is_write = (op_type in ["save", "export"])

        cid = self._app.statusbar.get_context_id("filehandling-message")
        self._statusbar_context_id = cid

        self._use_statusbar = bool(use_statusbar)
        self._use_dialogs = bool(use_dialogs)

        #: True only if the IO function run by call() succeeded.
        self.success = False

        self._progress_dialog = None
        self._progress_bar = None
        self._start_time = None
        self._last_pulse = None

    @with_wait_cursor
    def call(self, func, *args, **kwargs):
        """Call a save or load callable and watch its progress.

        :param callable func: The IO function to be called.
        :param \*args: Passed to func.
        :param \*\*kwargs: Passed to func.
        :returns: The return value of func.

        Messages about the operation in progress may be shown to the
        user according to the object's op_type and files_summary.  The
        supplied callable is called with a *args and **kwargs, plus a
        "progress" keyword argument that when updated will keep the UI
        managed by this object updated.

        If the callable returned, self.success is set to True. If it
        raised an exception, it will remain False.

        See also: lib.feedback.Progress.

        """
        statusbar = self._app.statusbar
        progress = lib.feedback.Progress()
        progress.changed += self._progress_changed_cb
        kwargs = kwargs.copy()
        kwargs["progress"] = progress

        cid = self._statusbar_context_id
        if self._use_statusbar:
            statusbar.remove_all(cid)
            statusbar.push(cid, self._duration_msg)

        self._start_time = self.clock_func()
        self._last_pulse = None
        result = None
        try:
            result = func(*args, **kwargs)
        except (FileHandlingError, AllocationError, MemoryError) as e:
            # Catch predictable exceptions here, and don't re-raise
            # them. Dialogs may be shown, but they will use
            # understandable language.
            logger.exception(
                u"IO failed (user-facing explanations: %s / %s)",
                self._fail_msg,
                unicode(e),
            )
            if self._use_statusbar:
                statusbar.remove_all(cid)
                self._app.show_transient_message(self._fail_msg)
            if self._use_dialogs:
                self._app.message_dialog(
                    title=self._fail_dialog_title,
                    text=self._fail_msg,
                    secondary_text=unicode(e),
                    message_type=Gtk.MessageType.ERROR,
                )
            self.success = False
        else:
            if result is False:
                logger.info("IO operation was cancelled by the user")
            else:
                logger.info("IO succeeded: %s", self._success_msg)
            if self._use_statusbar:
                statusbar.remove_all(cid)
                if result is not False:
                    self._app.show_transient_message(self._success_msg)
            self.success = result is not False
        finally:
            if self._progress_bar is not None:
                self._progress_dialog.destroy()
                self._progress_dialog = None
                self._progress_bar = None
        return result

    def _progress_changed_cb(self, progress):
        if self._progress_bar is None:
            now = self.clock_func()
            if (now - self._start_time) > 0.25:
                dialog = Gtk.Dialog(
                    title=self._duration_msg,
                    transient_for=self._app.drawWindow,
                    modal=True,
                    destroy_with_parent=True,
                )
                dialog.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
                dialog.set_decorated(False)
                style = dialog.get_style_context()
                style.add_class(Gtk.STYLE_CLASS_OSD)

                label = Gtk.Label()
                label.set_text(self._duration_msg)
                label.set_ellipsize(Pango.EllipsizeMode.MIDDLE)

                progress_bar = Gtk.ProgressBar()
                progress_bar.set_size_request(400, -1)

                dialog.vbox.set_border_width(16)
                dialog.vbox.set_spacing(8)
                dialog.vbox.pack_start(label, True, True, 0)
                dialog.vbox.pack_start(progress_bar, True, True, 0)

                progress_bar.show()
                dialog.show_all()
                self._progress_dialog = dialog
                self._progress_bar = progress_bar
                self._last_pulse = now

        self._update_progress_bar(progress)
        self._process_gtk_events()

    def _update_progress_bar(self, progress):
        if not self._progress_bar:
            return
        fraction = progress.fraction
        if fraction is None:
            now = self.clock_func()
            if (now - self._last_pulse) > 0.1:
                self._progress_bar.pulse()
                self._last_pulse = now
        else:
            self._progress_bar.set_fraction(fraction)

    def _process_gtk_events(self):
        while Gtk.events_pending():
            Gtk.main_iteration()


class FileHandler (object):
    """File handling object, part of the central app object.

    A single app-wide instance of this object is accessible from the
    central gui.application.Application instance as as app.filehandler.
    Several GTK action callbacks for opening and saving files reside
    here, and the object's public methods may be called from other parts
    of the application.

    NOTE: filehandling and drawwindow are very tightly coupled.

    """

    def __init__(self, app):
        self.app = app
        self.save_dialog = None

        # File filters definitions, for dialogs
        # (name, patterns)
        self.file_filters = [(
            C_(
                "save/load dialogs: filter patterns",
                u"All Recognized Formats",
            ), ["*.ora", "*.png", "*.jpg", "*.jpeg"],
        ), (
            C_(
                "save/load dialogs: filter patterns",
                u"OpenRaster (*.ora)",
            ), ["*.ora"],
        ), (
            C_(
                "save/load dialogs: filter patterns",
                u"PNG (*.png)",
            ), ["*.png"],
        ), (
            C_(
                "save/load dialogs: filter patterns",
                u"JPEG (*.jpg; *.jpeg)",
            ), ["*.jpg", "*.jpeg"],
        )]

        # Recent filter, for the menu.
        # Better to use a regex with re.IGNORECASE than
        # .upper()==.upper() hacks since internally, filenames are
        # Unicode and capitalization rules like Turkish's dotless "i"
        # exist. One day we want all the formats GdkPixbuf can load to
        # be supported in the dialog.

        file_regex_exts = set()
        for name, patts in self.file_filters:
            for p in patts:
                e = p.replace("*.", "", 1)
                file_regex_exts.add(re.escape(e))
        file_re = r'[.](?:' + ('|'.join(file_regex_exts)) + r')$'
        logger.debug("Using regex /%s/i for filtering recent files", file_re)
        self._file_extension_regex = re.compile(file_re, re.IGNORECASE)
        rf = Gtk.RecentFilter()
        rf.add_pattern('')
        # The blank-string pattern is eeded so the custom func will
        # get URIs at all, despite the needed flags below.
        rf.add_custom(
            func = self._recentfilter_func,
            needed = (
                Gtk.RecentFilterFlags.APPLICATION |
                Gtk.RecentFilterFlags.URI
            )
        )
        ra = app.find_action("OpenRecent")
        ra.add_filter(rf)

        ag = app.builder.get_object('FileActions')
        for action in ag.list_actions():
            self.app.kbm.takeover_action(action)

        self._filename = None
        self.current_file_observers = []
        self.file_opened_observers = []
        self.active_scrap_filename = None
        self.lastsavefailed = False
        self._update_recent_items()

        # { FORMAT: (name, extension, options) }
        self.saveformats = OrderedDict([
            (_SaveFormat.ANY, (C_(
                "save dialogs: save formats and options",
                u"By extension (prefer default format)",
            ), None, {})),
            (_SaveFormat.ORA, (C_(
                "save dialogs: save formats and options",
                u"OpenRaster (*.ora)",
            ), '.ora', {})),
            (_SaveFormat.PNG_AUTO, (C_(
                "save dialogs: save formats and options",
                u"PNG, respecting “Show Background” (*.png)"
            ), '.png', {})),
            (_SaveFormat.PNG_SOLID, (C_(
                "save dialogs: save formats and options",
                u"PNG, solid RGB (*.png)",
            ), '.png', {'alpha': False})),
            (_SaveFormat.PNG_TRANS, (C_(
                "save dialogs: save formats and options",
                u"PNG, transparent RGBA (*.png)",
            ), '.png', {'alpha': True})),
            (_SaveFormat.PNGS_BY_LAYER, (C_(
                "save dialogs: save formats and options",
                u"Multiple PNGs, by layer (*.NUM.png)",
            ), '.png', {'multifile': 'layers'})),
            (_SaveFormat.PNGS_BY_VIEW, (C_(
                "save dialogs: save formats and options",
                u"Multiple PNGs, by view (*.NAME.png)",
            ), '.png', {'multifile': 'views'})),
            (_SaveFormat.JPEG, (C_(
                "save dialogs: save formats and options",
                u"JPEG 90% quality (*.jpg; *.jpeg)",
            ), '.jpg', {'quality': 90})),
        ])
        self.ext2saveformat = {
            ".ora": (_SaveFormat.ORA, "image/openraster"),
            ".png": (_SaveFormat.PNG_AUTO, "image/png"),
            ".jpeg": (_SaveFormat.JPEG, "image/jpeg"),
            ".jpg": (_SaveFormat.JPEG, "image/jpeg"),
        }
        self.config2saveformat = {
            'openraster': _SaveFormat.ORA,
            'jpeg-90%': _SaveFormat.JPEG,
            'png-solid': _SaveFormat.PNG_SOLID,
        }

    def _update_recent_items(self):
        """Updates self._recent_items from the GTK RecentManager.

        This list is consumed in open_last_cb.

        """
        # Note: i.exists() does not work on Windows if the pathname
        # contains utf-8 characters. Since GIMP also saves its URIs
        # with utf-8 characters into this list, I assume this is a
        # gtk bug.  So we use our own test instead of i.exists().

        recent_items = []
        rm = Gtk.RecentManager.get_default()
        for i in rm.get_items():
            if not i:
                continue
            apps = i.get_applications()
            if not (apps and "mypaint" in apps):
                continue
            if self._uri_is_loadable(i.get_uri()):
                recent_items.append(i)
        # This test should be kept in sync with _recentfilter_func.
        recent_items.reverse()
        self._recent_items = recent_items

    def get_filename(self):
        return self._filename

    def set_filename(self, value):
        self._filename = value
        for f in self.current_file_observers:
            f(self.filename)

        if self.filename:
            if self.filename.startswith(self.get_scrap_prefix()):
                self.active_scrap_filename = self.filename

    filename = property(get_filename, set_filename)

    def init_save_dialog(self, export):
        if export:
            save_dialog_name = C_(
                "Dialogs (window title): File→Export…",
                u"Export"
            )
        else:
            save_dialog_name = C_(
                "Dialogs (window title): File→Save As…",
                u"Save As"
            )
        dialog = Gtk.FileChooserDialog(
            save_dialog_name,
            self.app.drawWindow,
            Gtk.FileChooserAction.SAVE,
            (
                Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                Gtk.STOCK_SAVE, Gtk.ResponseType.OK,
            ),
        )
        dialog.set_default_response(Gtk.ResponseType.OK)
        dialog.set_do_overwrite_confirmation(True)
        _add_filters_to_dialog(self.file_filters, dialog)

        # Add widget for selecting save format
        box = Gtk.HBox()
        box.set_spacing(12)
        label = Gtk.Label(label=C_(
            "save dialogs: formats and options: (label)",
            u"Format to save as:",
        ))
        label.set_alignment(0.0, 0.5)
        combo = Gtk.ComboBoxText()
        for (name, ext, opt) in self.saveformats.values():
            combo.append_text(name)
        combo.set_active(0)
        combo.connect('changed', self.selected_save_format_changed_cb)
        self.saveformat_combo = combo

        box.pack_start(label, True, True, 0)
        box.pack_start(combo, False, True, 0)
        dialog.set_extra_widget(box)
        dialog.show_all()
        return dialog

    def selected_save_format_changed_cb(self, widget):
        """When the user changes the selected format to save as in the dialog,
        change the extension of the filename (if existing) immediately."""
        dialog = self.save_dialog
        filename = dialog.get_filename()
        if filename:
            filename = filename_to_unicode(filename)
            filename, ext = os.path.splitext(filename)
            if ext:
                saveformat = self.saveformat_combo.get_active()
                ext = self.saveformats[saveformat][1]
                if ext is not None:
                    _dialog_set_filename(dialog, filename + ext)

    def confirm_destructive_action(self, title=None, confirm=None,
                                   offer_save=True):
        """Asks the user to confirm an action that might lose work.

        :param unicode title: Short question to ask the user.
        :param unicode confirm: Imperative verb for the "do it" button.
        :param bool offer_save: Set False to turn off the save checkbox.
        :rtype: bool
        :returns: True if the user allows the destructive action

        Phrase the title question tersely.
        In English/source, use title case for it, and with a question mark.
        Good examples are “Really Quit?”,
        or “Delete Everything?”.
        The title should always tell the user
        what destructive action is about to take place.
        If it is not specified, a default title is used.

        Use a single, specific, imperative verb for the confirm string.
        It should reflect the title question.
        This is used for the primary confirmation button, if specified.
        See the GNOME HIG for further guidelines on what to use here.

        This method doesn't bother asking
        if there's less than a handful of seconds of unsaved work.
        By default, that's 1 second.
        The build-time and runtime debugging flags
        make this period longer
        to allow more convenient development and testing.

        Ref: https://developer.gnome.org/hig/stable/dialogs.html.en

        """
        if title is None:
            title = C_(
                "Destructive action confirm dialog: "
                "fallback title (normally overridden)",
                "Really Continue?"
            )

        # Get an accurate assessment of how much change is unsaved.
        self.doc.model.sync_pending_changes()
        t = self.doc.model.unsaved_painting_time

        # This used to be 30, but see https://gna.org/bugs/?17955
        # Then 8 by default, but Twitter users hate that too.
        t_bother = 1
        if mypaintlib.heavy_debug:
            t_bother += 7
        if os.environ.get("MYPAINT_DEBUG", False):
            t_bother += 7
        logger.debug("Destructive action don't-bother period is %ds", t_bother)
        if t < t_bother:
            return True

        # Custom response codes.
        # The default ones are all negative ints.
        continue_response_code = 1

        # Dialog setup.
        d = Gtk.MessageDialog(
            title=title,
            transient_for=self.app.drawWindow,
            message_type=Gtk.MessageType.QUESTION,
            modal=True
        )

        # Translated strings for things
        cancel_btn_text = C_(
            "Destructive action confirm dialog: cancel button",
            u"_Cancel",
        )
        save_to_scraps_first_text = C_(
            "Destructive action confirm dialog: save checkbox",
            u"_Save to Scraps first",
        )
        if not confirm:
            continue_btn_text = C_(
                "Destructive action confirm dialog: "
                "fallback continue button (normally overridden)",
                u"Co_ntinue",
            )
        else:
            continue_btn_text = confirm

        # Button setup. Cancel first, continue at end.
        buttons = [
            (cancel_btn_text, Gtk.ResponseType.CANCEL, False),
            (continue_btn_text, continue_response_code, True),
        ]
        for txt, code, destructive in buttons:
            button = d.add_button(txt, code)
            styles = button.get_style_context()
            if destructive:
                styles.add_class(Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION)

        # Explanatory message.
        if self.filename:
            file_basename = os.path.basename(self.filename)
        else:
            file_basename = None
        warning_msg_tmpl = C_(
            "Destructive action confirm dialog: warning message",
            u"You risk losing {abbreviated_time} of unsaved painting."
        )
        markup_tmpl = warning_msg_tmpl
        d.set_markup(markup_tmpl.format(
            abbreviated_time = lib.xml.escape(helpers.fmt_time_period_abbr(t)),
            current_file_name = lib.xml.escape(file_basename),
        ))

        # Checkbox for saving
        if offer_save:
            save1st_text = save_to_scraps_first_text
            save1st_cb = Gtk.CheckButton.new_with_mnemonic(save1st_text)
            save1st_cb.set_hexpand(False)
            save1st_cb.set_halign(Gtk.Align.END)
            save1st_cb.set_vexpand(False)
            save1st_cb.set_margin_top(12)
            save1st_cb.set_margin_bottom(12)
            save1st_cb.set_margin_start(12)
            save1st_cb.set_margin_end(12)
            save1st_cb.set_can_focus(False)  # set back again in show handler
            d.connect(
                "show",
                self._destructive_action_dialog_show_cb,
                save1st_cb,
            )
            save1st_cb.connect(
                "toggled",
                self._destructive_action_dialog_save1st_toggled_cb,
                d,
            )
            vbox = d.get_content_area()
            vbox.set_spacing(0)
            vbox.set_margin_top(12)
            vbox.pack_start(save1st_cb, False, True, 0)

        # Get a response and handle it.
        d.set_default_response(Gtk.ResponseType.CANCEL)
        response_code = d.run()
        d.destroy()
        if response_code == continue_response_code:
            logger.debug("Destructive action confirmed")
            if offer_save and save1st_cb.get_active():
                logger.info("Saving current canvas as a new scrap")
                self.save_scrap_cb(None)
            return True
        else:
            logger.debug("Destructive action cancelled")
            return False

    def _destructive_action_dialog_show_cb(self, dialog, checkbox):
        checkbox.show_all()
        checkbox.set_can_focus(True)

    def _destructive_action_dialog_save1st_toggled_cb(self, checkbox, dialog):
        # Choosing to save locks you into a particular course of action.
        # Hopefully this isn't too strange.
        # Escape will still work.
        cancel_allowed = not checkbox.get_active()
        cancel_btn = dialog.get_widget_for_response(Gtk.ResponseType.CANCEL)
        cancel_btn.set_sensitive(cancel_allowed)

    def new_cb(self, action):
        ok_to_start_new_doc = self.confirm_destructive_action(
            title = C_(
                u'File→New: confirm dialog: title question',
                u"New Canvas?",
            ),
            confirm = C_(
                u'File→New: confirm dialog: continue button',
                u"_New Canvas",
            ),
        )
        if not ok_to_start_new_doc:
            return
        self.app.reset_compat_mode()
        self.doc.reset_background()
        self.doc.model.clear()
        self.filename = None
        self._update_recent_items()
        self.app.doc.reset_view(True, True, True)

    @staticmethod
    def gtk_main_tick(*args, **kwargs):
        while Gtk.events_pending():
            Gtk.main_iteration()

    def open_file(self, filename, **kwargs):
        """Load a file, replacing the current working document."""
        if not self._call_doc_load_method(
                self.doc.model.load, filename, False, **kwargs):
            # Without knowledge of _when_ the process failed, clear
            # the document to make sure we're not in an inconsistent state.
            # TODO: Improve the control flow to permit a less draconian
            # approach, for exceptions occurring prior to any doc-changes.
            self.filename = None
            self.app.reset_compat_mode()
            self.doc.model.clear()
            return

        self.filename = os.path.abspath(filename)
        for func in self.file_opened_observers:
            func(self.filename)
        logger.info('Loaded from %r', self.filename)
        self.app.doc.reset_view(True, True, True)
        # try to restore the last used brush and color
        layers = self.doc.model.layer_stack
        search_layers = []
        if layers.current is not None:
            search_layers.append(layers.current)
        search_layers.extend(layers.deepiter())
        for layer in search_layers:
            si = layer.get_last_stroke_info()
            if si:
                self.app.restore_brush_from_stroke_info(si)
                break

    def import_layers(self, filenames):
        """Load a file, replacing the current working document."""

        if not self._call_doc_load_method(self.doc.model.import_layers,
                                          filenames, True):
            return
        logger.info('Imported layers from %r', filenames)

    def _call_doc_load_method(
            self, method, arg, is_import, compat_handler=None):
        """Internal: common GUI aspects of loading or importing files.

        Calls a document model loader method (on lib.document.Document)
        with the given argument. Catches common loading exceptions and
        shows appropriate error messages.

        """
        if not compat_handler:
            compat_handler = compat.ora_compat_handler(self.app)
        prefs = self.app.preferences
        display_colorspace_setting = prefs["display.colorspace"]

        op_type = is_import and "import" or "load"

        files_summary = _IOProgressUI.format_files_summary(arg)
        ioui = _IOProgressUI(self.app, op_type, files_summary)
        result = ioui.call(
            method, arg,
            convert_to_srgb=(display_colorspace_setting == "srgb"),
            compat_handler=compat_handler,
            incompatible_ora_cb=compat.incompatible_ora_cb(self.app)
        )
        return (result is not False) and ioui.success

    def open_scratchpad(self, filename):
        no_ui_progress = lib.feedback.Progress()
        no_ui_progress.changed += self.gtk_main_tick
        try:
            self.app.scratchpad_doc.model.load(
                filename,
                progress=no_ui_progress,
            )
            self.app.scratchpad_filename = os.path.abspath(filename)
            self.app.preferences["scratchpad.last_opened_scratchpad"] \
                = self.app.scratchpad_filename
        except (FileHandlingError, AllocationError, MemoryError) as e:
            self.app.message_dialog(
                unicode(e),
                message_type=Gtk.MessageType.ERROR
            )
        else:
            self.app.scratchpad_filename = os.path.abspath(filename)
            self.app.preferences["scratchpad.last_opened_scratchpad"] \
                = self.app.scratchpad_filename
            logger.info('Loaded scratchpad from %r',
                        self.app.scratchpad_filename)
            self.app.scratchpad_doc.reset_view(True, True, True)

    def save_file(self, filename, export=False, **options):
        """Saves the main document to one or more files (app/toplevel)

        :param filename: The base filename to save
        :param bool export: True if exporting
        :param **options: Pass-through options

        This method invokes `_save_doc_to_file()` with the main working
        doc, but also attempts to save thumbnails and perform recent
        files list management, when appropriate.

        See `_save_doc_to_file()`
        """
        thumbnail_pixbuf = self._save_doc_to_file(
            filename,
            self.doc,
            export=export,
            use_statusbar=True,
            **options
        )
        if "multifile" in options:  # thumbs & recents are inappropriate
            return
        if not os.path.isfile(filename):  # failed to save
            return
        if not export:
            self.filename = os.path.abspath(filename)
            basename, ext = os.path.splitext(self.filename)
            recent_mgr = Gtk.RecentManager.get_default()
            uri = lib.glib.filename_to_uri(self.filename)
            recent_data = Gtk.RecentData()
            recent_data.app_name = "mypaint"
            app_exec = sys.argv_unicode[0]
            assert isinstance(app_exec, unicode)
            recent_data.app_exec = app_exec
            mime_default = "application/octet-stream"
            fmt, mime_type = self.ext2saveformat.get(ext, (None, mime_default))
            recent_data.mime_type = mime_type
            recent_mgr.add_full(uri, recent_data)
        if not thumbnail_pixbuf:
            options["render_background"] = not options.get("alpha", False)
            thumbnail_pixbuf = self.doc.model.render_thumbnail(**options)
        helpers.freedesktop_thumbnail(filename, thumbnail_pixbuf)

    @with_wait_cursor
    def save_scratchpad(self, filename, export=False, **options):
        save_needed = (
            self.app.scratchpad_doc.model.unsaved_painting_time
            or export
            or not os.path.exists(filename)
        )
        if save_needed:
            self._save_doc_to_file(
                filename,
                self.app.scratchpad_doc,
                export=export,
                use_statusbar=False,
                **options
            )
        if not export:
            self.app.scratchpad_filename = os.path.abspath(filename)
            self.app.preferences["scratchpad.last_opened_scratchpad"] \
                = self.app.scratchpad_filename

    def _save_doc_to_file(self, filename, doc, export=False,
                          use_statusbar=True,
                          **options):
        """Saves a document to one or more files

        :param filename: The base filename to save
        :param Document doc: Controller for the document to save
        :param bool export: True if exporting
        :param **options: Pass-through options

        This method handles logging, statusbar messages,
        and alerting the user to when the save failed.

        See also: lib.document.Document.save(), _IOProgressUI.
        """
        thumbnail_pixbuf = None
        prefs = self.app.preferences
        display_colorspace_setting = prefs["display.colorspace"]
        options['save_srgb_chunks'] = (display_colorspace_setting == "srgb")

        files_summary = _IOProgressUI.format_files_summary(filename)
        op_type = export and "export" or "save"
        ioui = _IOProgressUI(self.app, op_type, files_summary,
                             use_statusbar=use_statusbar)

        thumbnail_pixbuf = ioui.call(doc.model.save, filename, **options)
        self.lastsavefailed = not ioui.success
        return thumbnail_pixbuf

    def update_preview_cb(self, file_chooser, preview):
        filename = file_chooser.get_preview_filename()
        if filename:
            filename = filename_to_unicode(filename)
            pixbuf = helpers.freedesktop_thumbnail(filename)
            if pixbuf:
                # if pixbuf is smaller than 256px in width, copy it onto
                # a transparent 256x256 pixbuf
                pixbuf = helpers.pixbuf_thumbnail(pixbuf, 256, 256, True)
                preview.set_from_pixbuf(pixbuf)
                file_chooser.set_preview_widget_active(True)
            else:
                # TODO: display "no preview available" image?
                file_chooser.set_preview_widget_active(False)

    def open_cb(self, action):
        ok_to_open = self.app.filehandler.confirm_destructive_action(
            title = C_(
                u'File→Open: confirm dialog: title question',
                u"Open File?",
            ),
            confirm = C_(
                u'File→Open: confirm dialog: continue button',
                u"_Open…",
            ),
        )
        if not ok_to_open:
            return
        dialog = Gtk.FileChooserDialog(
            title=C_(
                u'File→Open: file chooser dialog: title',
                u"Open File",
            ),
            transient_for=self.app.drawWindow,
            action=Gtk.FileChooserAction.OPEN,
        )
        dialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
        dialog.add_button(Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
        dialog.set_default_response(Gtk.ResponseType.OK)

        # Compatibility override options for .ora files
        selector = compat.CompatSelector(self.app)
        dialog.connect('selection-changed', selector.file_selection_changed_cb)
        dialog.set_extra_widget(selector.widget)

        preview = Gtk.Image()
        dialog.set_preview_widget(preview)
        dialog.connect("update-preview", self.update_preview_cb, preview)

        _add_filters_to_dialog(self.file_filters, dialog)

        if self.filename:
            dialog.set_filename(self.filename)
        else:
            # choose the most recent save folder
            self._update_recent_items()
            for item in reversed(self._recent_items):
                uri = item.get_uri()
                fn, _h = lib.glib.filename_from_uri(uri)
                dn = os.path.dirname(fn)
                if os.path.isdir(dn):
                    dialog.set_current_folder(dn)
                    break
        try:
            if dialog.run() == Gtk.ResponseType.OK:
                dialog.hide()
                filename = dialog.get_filename()
                filename = filename_to_unicode(filename)
                self.open_file(
                    filename,
                    compat_handler=selector.compat_function
                )
        finally:
            dialog.destroy()

    def open_scratchpad_dialog(self):
        dialog = Gtk.FileChooserDialog(
            C_(
                "load dialogs: title",
                u"Open Scratchpad…",
            ),
            self.app.drawWindow,
            Gtk.FileChooserAction.OPEN,
            (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
             Gtk.STOCK_OPEN, Gtk.ResponseType.OK),
        )
        dialog.set_default_response(Gtk.ResponseType.OK)

        preview = Gtk.Image()
        dialog.set_preview_widget(preview)
        dialog.connect("update-preview", self.update_preview_cb, preview)

        _add_filters_to_dialog(self.file_filters, dialog)

        if self.app.scratchpad_filename:
            dialog.set_filename(self.app.scratchpad_filename)
        else:
            # choose the most recent save folder
            self._update_recent_items()
            for item in reversed(self._recent_items):
                uri = item.get_uri()
                fn, _h = lib.glib.filename_from_uri(uri)
                dn = os.path.dirname(fn)
                if os.path.isdir(dn):
                    dialog.set_current_folder(dn)
                    break
        try:
            if dialog.run() == Gtk.ResponseType.OK:
                dialog.hide()
                filename = dialog.get_filename()
                filename = filename_to_unicode(filename)
                self.app.scratchpad_filename = filename
                self.open_scratchpad(filename)
        finally:
            dialog.destroy()

    def import_layers_cb(self, action):
        """Action callback: import layers from multiple files."""
        dialog = Gtk.FileChooserDialog(
            title = C_(
                u'Layers→Import Layers: files-chooser dialog: title',
                u"Import Layers",
            ),
            parent = self.app.drawWindow,
            action = Gtk.FileChooserAction.OPEN,
        )
        dialog.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
        dialog.add_button(Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
        dialog.set_default_response(Gtk.ResponseType.OK)

        dialog.set_select_multiple(True)

        # TODO: decide how well the preview plays with multiple-select.
        preview = Gtk.Image()
        dialog.set_preview_widget(preview)
        dialog.connect("update-preview", self.update_preview_cb, preview)

        _add_filters_to_dialog(self.file_filters, dialog)

        # Choose the most recent save folder.
        self._update_recent_items()
        for item in reversed(self._recent_items):
            uri = item.get_uri()
            fn, _h = lib.glib.filename_from_uri(uri)
            dn = os.path.dirname(fn)
            if os.path.isdir(dn):
                dialog.set_current_folder(dn)
                break

        filenames = []
        try:
            if dialog.run() == Gtk.ResponseType.OK:
                dialog.hide()
                filenames = dialog.get_filenames()
        finally:
            dialog.destroy()

        if filenames:
            filenames = [filename_to_unicode(f) for f in filenames]
            self.import_layers(filenames)

    def save_cb(self, action):
        if not self.filename:
            self.save_as_cb(action)
        else:
            self.save_file(self.filename)

    def save_as_cb(self, action):
        if self.filename:
            current_filename = self.filename
        else:
            current_filename = ''
            # choose the most recent save folder
            self._update_recent_items()
            for item in reversed(self._recent_items):
                uri = item.get_uri()
                fn, _h = lib.glib.filename_from_uri(uri)
                dn = os.path.dirname(fn)
                if os.path.isdir(dn):
                    break

        self.save_as_dialog(
            self.save_file,
            suggested_filename=current_filename,
            export = (action.get_name() == 'Export'),
        )

    def save_scratchpad_as_dialog(self, export=False):
        if self.app.scratchpad_filename:
            current_filename = self.app.scratchpad_filename
        else:
            current_filename = ''

        self.save_as_dialog(
            self.save_scratchpad,
            suggested_filename=current_filename,
            export=export,
        )

    def save_as_dialog(self, save_method_reference, suggested_filename=None,
                       start_in_folder=None, export=False,
                       **options):
        if not self.save_dialog:
            self.save_dialog = self.init_save_dialog(export)
        dialog = self.save_dialog
        # Set the filename in the dialog
        if suggested_filename:
            _dialog_set_filename(dialog, suggested_filename)
        else:
            _dialog_set_filename(dialog, '')
            # Recent directory?
            if start_in_folder:
                dialog.set_current_folder(start_in_folder)

        try:
            # Loop until we have filename with an extension
            while dialog.run() == Gtk.ResponseType.OK:
                filename = dialog.get_filename()
                if filename is None:
                    continue
                filename = filename_to_unicode(filename)
                name, ext = os.path.splitext(filename)
                saveformat = self.saveformat_combo.get_active()

                # If no explicitly selected format, use the extension to
                # figure it out
                if saveformat == _SaveFormat.ANY:
                    cfg = self.app.preferences['saving.default_format']
                    default_saveformat = self.config2saveformat[cfg]
                    if ext:
                        try:
                            saveformat, mime = self.ext2saveformat[ext]
                        except KeyError:
                            saveformat = default_saveformat
                    else:
                        saveformat = default_saveformat

                # if saveformat isn't a key, it must be SAVE_FORMAT_PNGAUTO.
                desc, ext_format, options = self.saveformats.get(
                    saveformat,
                    ("", ext, {'alpha': None}),
                )

                if ext:
                    if ext_format != ext:
                        # Minor ugliness: if the user types '.png' but
                        # leaves the default .ora filter selected, we
                        # use the default options instead of those
                        # above. However, they are the same at the moment.
                        options = {}
                    assert(filename)
                    dialog.hide()
                    if export:
                        # Do not change working file
                        save_method_reference(filename, True, **options)
                    else:
                        save_method_reference(filename, **options)
                    break

                filename = name + ext_format

                # trigger overwrite confirmation for the modified filename
                _dialog_set_filename(dialog, filename)
                dialog.response(Gtk.ResponseType.OK)

        finally:
            dialog.hide()
            dialog.destroy()  # avoid GTK crash: https://gna.org/bugs/?17902
            self.save_dialog = None

    def save_scrap_cb(self, action):
        filename = self.filename
        prefix = self.get_scrap_prefix()
        self.app.filename = self.save_autoincrement_file(
            filename,
            prefix,
            main_doc=True,
        )

    def save_scratchpad_cb(self, action):
        filename = self.app.scratchpad_filename
        prefix = self.get_scratchpad_prefix()
        self.app.scratchpad_filename = self.save_autoincrement_file(
            filename,
            prefix,
            main_doc=False,
        )

    def save_autoincrement_file(self, filename, prefix, main_doc=True):
        # If necessary, create the folder(s) the scraps are stored under
        prefix_dir = os.path.dirname(prefix)
        if not os.path.exists(prefix_dir):
            os.makedirs(prefix_dir)

        number = None
        if filename:
            junk, file_fragment = os.path.split(filename)
            if file_fragment.startswith("_md5"):
                # store direct, don't attempt to increment
                if main_doc:
                    self.save_file(filename)
                else:
                    self.save_scratchpad(filename)
                return filename

            found_nums = re.findall(re.escape(prefix) + '([0-9]+)', filename)
            if found_nums:
                number = found_nums[0]

        if number:
            # reuse the number, find the next character
            char = 'a'
            for filename in glob(prefix + number + '_*'):
                c = filename[len(prefix + number + '_')]
                if c >= 'a' and c <= 'z' and c >= char:
                    char = chr(ord(c) + 1)
            if char > 'z':
                # out of characters, increase the number
                filename = None
                return self.save_autoincrement_file(filename, prefix, main_doc)
            filename = '%s%s_%c' % (prefix, number, char)
        else:
            # we don't have a scrap filename yet, find the next number
            maximum = 0
            for filename in glob(prefix + '[0-9][0-9][0-9]*'):
                filename = filename[len(prefix):]
                res = re.findall(r'[0-9]*', filename)
                if not res:
                    continue
                number = int(res[0])
                if number > maximum:
                    maximum = number
            filename = '%s%03d_a' % (prefix, maximum + 1)

        # Add extension
        cfg = self.app.preferences['saving.default_format']
        default_saveformat = self.config2saveformat[cfg]
        filename += self.saveformats[default_saveformat][1]

        assert not os.path.exists(filename)
        if main_doc:
            self.save_file(filename)
        else:
            self.save_scratchpad(filename)
        return filename

    def get_scrap_prefix(self):
        prefix = self.app.preferences['saving.scrap_prefix']
        # This should really use two separate settings, not one.
        # https://github.com/mypaint/mypaint/issues/375
        prefix = fileutils.expanduser_unicode(prefix)
        prefix = os.path.abspath(prefix)
        if os.path.isdir(prefix):
            if not prefix.endswith(os.path.sep):
                prefix += os.path.sep
        return prefix

    def get_scratchpad_prefix(self):
        # TODO allow override via prefs, maybe
        prefix = os.path.join(self.app.user_datapath, 'scratchpads')
        prefix = os.path.abspath(prefix)
        if os.path.isdir(prefix):
            if not prefix.endswith(os.path.sep):
                prefix += os.path.sep
        return prefix

    def get_scratchpad_default(self):
        # TODO get the default name from preferences
        prefix = self.get_scratchpad_prefix()
        return os.path.join(prefix, "scratchpad_default.ora")

    def get_scratchpad_autosave(self):
        prefix = self.get_scratchpad_prefix()
        return os.path.join(prefix, "autosave.ora")

    def list_scraps(self):
        prefix = self.get_scrap_prefix()
        return self._list_prefixed_dir(prefix)

    def list_scratchpads(self):
        prefix = self.get_scratchpad_prefix()
        files = self._list_prefixed_dir(prefix)
        special_prefix = os.path.join(prefix, "special")
        if os.path.isdir(special_prefix):
            files += self._list_prefixed_dir(special_prefix + os.path.sep)
        return files

    def _list_prefixed_dir(self, prefix):
        filenames = []
        for ext in ['png', 'ora', 'jpg', 'jpeg']:
            filenames += glob(prefix + '[0-9]*.' + ext)
            filenames += glob(prefix + '[0-9]*.' + ext.upper())
            # For the special linked scratchpads
            filenames += glob(prefix + '_md5[0-9a-f]*.' + ext)
        filenames.sort()
        return filenames

    def list_scraps_grouped(self):
        filenames = self.list_scraps()
        return self.list_files_grouped(filenames)

    def list_scratchpads_grouped(self):
        filenames = self.list_scratchpads()
        return self.list_files_grouped(filenames)

    def list_files_grouped(self, filenames):
        """return scraps grouped by their major number"""
        def scrap_id(filename):
            s = os.path.basename(filename)
            if s.startswith("_md5"):
                return s
            return re.findall('([0-9]+)', s)[0]
        groups = []
        while filenames:
            group = []
            sid = scrap_id(filenames[0])
            while filenames and scrap_id(filenames[0]) == sid:
                group.append(filenames.pop(0))
            groups.append(group)
        return groups

    def open_recent_cb(self, action):
        """Callback for RecentAction"""
        uri = action.get_current_uri()
        fn, _h = lib.glib.filename_from_uri(uri)
        ok_to_open = self.app.filehandler.confirm_destructive_action(
            title = C_(
                u'File→Open Recent→* confirm dialog: title',
                u"Open File?"
            ),
            confirm = C_(
                u'File→Open Recent→* confirm dialog: continue button',
                u"_Open"
            ),
        )
        if not ok_to_open:
            return
        self.open_file(fn)

    def open_last_cb(self, action):
        """Callback to open the last file"""
        if not self._recent_items:
            return
        ok_to_open = self.app.filehandler.confirm_destructive_action(
            title = C_(
                u'File→Open Most Recent confirm dialog: '
                u'title',
                u"Open Most Recent File?",
            ),
            confirm = C_(
                u'File→Open Most Recent→* confirm dialog: '
                u'continue button',
                u"_Open"
            ),
        )
        if not ok_to_open:
            return
        uri = self._recent_items.pop().get_uri()
        fn, _h = lib.glib.filename_from_uri(uri)
        self.open_file(fn)

    def open_scrap_cb(self, action):
        groups = self.list_scraps_grouped()
        if not groups:
            msg = C_(
                'File→Open Next/Prev Scrap: error message',
                u"There are no scrap files yet. Try saving one first.",
            )
            self.app.message_dialog(msg, message_type=Gtk.MessageType.WARNING)
            return
        next = action.get_name() == 'NextScrap'
        if next:
            dialog_title = C_(
                u'File→Open Next/Prev Scrap confirm dialog: '
                u'title',
                u"Open Next Scrap?"
            )
            idx = 0
            delta = 1
        else:
            dialog_title = C_(
                u'File→Open Next/Prev Scrap confirm dialog: '
                u'title',
                u"Open Previous Scrap?"
            )
            idx = -1
            delta = -1
        ok_to_open = self.app.filehandler.confirm_destructive_action(
            title = dialog_title,
            confirm = C_(
                u'File→Open Next/Prev Scrap confirm dialog: '
                u'continue button',
                u"_Open"
            ),
        )
        if not ok_to_open:
            return
        for i, group in enumerate(groups):
            if self.active_scrap_filename in group:
                idx = i + delta
        filename = groups[idx % len(groups)][-1]
        self.open_file(filename)

    def reload_cb(self, action):
        if not self.filename:
            self.app.show_transient_message(C_(
                u'File→Revert: status message: canvas has no filename yet',
                u"Cannot revert: canvas has not been saved to a file yet.",
            ))
            return
        ok_to_reload = self.app.filehandler.confirm_destructive_action(
            title = C_(
                u'File→Revert confirm dialog: '
                u'title',
                u"Revert Changes?",
            ),
            confirm = C_(
                u'File→Revert confirm dialog: '
                u'continue button',
                u"_Revert"
            ),
        )
        if ok_to_reload:
            self.open_file(self.filename)

    def delete_scratchpads(self, filenames):
        prefix = self.get_scratchpad_prefix()
        prefix = os.path.abspath(prefix)
        for filename in filenames:
            if not (os.path.isfile(filename) and
                    os.path.abspath(filename).startswith(prefix)):
                continue
            os.remove(filename)
            logger.info("Removed %s", filename)

    def delete_default_scratchpad(self):
        if os.path.isfile(self.get_scratchpad_default()):
            os.remove(self.get_scratchpad_default())
            logger.info("Removed the scratchpad default file")

    def delete_autosave_scratchpad(self):
        if os.path.isfile(self.get_scratchpad_autosave()):
            os.remove(self.get_scratchpad_autosave())
            logger.info("Removed the scratchpad autosave file")

    def _recentfilter_func(self, rfinfo):
        """Recent-file filter function.

        This does a filename extension check, and also verifies that the
        file actually exists.

        """
        if not rfinfo:
            return False
        apps = rfinfo.applications
        if not (apps and "mypaint" in apps):
            return False
        return self._uri_is_loadable(rfinfo.uri)
        # Keep this test in sync with _update_recent_items().

    def _uri_is_loadable(self, file_uri):
        """True if a URI is valid to be loaded by MyPaint."""
        if file_uri is None:
            return False
        if not file_uri.startswith("file://"):
            return False
        file_path, _host = lib.glib.filename_from_uri(file_uri)
        if not os.path.exists(file_path):
            return False
        if not self._file_extension_regex.search(file_path):
            return False
        return True