~emihalac/mnemosyne-proj/mnemosyne-proj

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
#
# default_controller.py <Peter.Bienstman@UGent.be>
#

import os
import sys
import copy
import time
import shutil

from mnemosyne.libmnemosyne.fact import Fact
from mnemosyne.libmnemosyne.translator import _
from mnemosyne.libmnemosyne.controller import Controller
from mnemosyne.libmnemosyne.utils import remove_empty_dirs_in
from mnemosyne.libmnemosyne.utils import expand_path, contract_path

HOUR = 60 * 60 # Seconds in an hour.
DAY = 24 * HOUR # Seconds in a day.


class DefaultController(Controller):

    """A collection of logic used by the GUI.  The logic related to the
    review process is split out in a separated controller class, to
    allow that to be swapped out easily.

    There are two classes of functions here: 'show_XXX_dialog', which needs
    to be called by the GUI to set everything up to show a certain dialog,
    and then the other functions, which the implementation of the actual
    dialog can use to perform GUI-independent operations.

    See also 'How to write a new frontend' in the docs.

    """

    def activate(self):
        self.study_mode = None
        Controller.activate(self)
        self.next_rollover = self.database().start_of_day_n_days_ago(n=-1)
                
    def heartbeat(self, db_maintenance=True):

        """Making sure, even if the user leaves the program open indefinitely,
        that backups get taken, that the cards scheduled for the day get dumped
        to the log and that the the logs get uploaded, and that the new cards
        for the day are brought into the queue.

        """

        if time.time() > self.next_rollover:
            self.flush_sync_server()
            if not self.database().is_loaded() or \
                not self.database().is_accessible():
                # Make sure we don't continue if e.g. the GUI or another thread
                # holds the database
                return
            self.database().backup()
            self.log().saved_database()
            self.log().loaded_database()
            self.log().future_schedule()
            self.log().dump_to_science_log()
            self.log().deactivate()
            self.log().activate()
            self.config().save()
            self.reset_study_mode()
            self.next_rollover = self.database().start_of_day_n_days_ago(n=-1)
        if db_maintenance and \
           (time.time() > self.config()["last_db_maintenance"] + 90 * DAY):
            self.component_manager.current("database_maintenance").run()        
            self.config()["last_db_maintenance"] = time.time()
            self.config().save()
            
    def do_db_maintenance(self):
        if time.time() < self.config()["last_db_maintenance"] + 30 * DAY:
            self.main_widget().show_information(\
          _("No need to do database maintenance more than once per month."))
            return
        else:
            self.component_manager.current("database_maintenance").run()        
            self.config()["last_db_maintenance"] = time.time()
            self.config().save()

    def update_title(self):
        title = _("Mnemosyne")
        db = self.database()
        database_name = db.display_name()
        if database_name and database_name != db.default_name:
            title += " - " + database_name
        if db.current_criterion() and \
            db.current_criterion().name and \
            db.current_criterion().name != db.default_criterion_name:
            title += " - " + db.current_criterion().name
        self.main_widget().set_window_title(title)
        
    def set_study_mode(self, study_mode):
        if self.study_mode == study_mode:
            return
        if self.study_mode is not None:
            self.study_mode.deactivate()        
        study_mode.activate()        
        self.study_mode = study_mode
        self.config()["study_mode"] = study_mode.id
        
    def reset_study_mode(self, study_mode):
        self.study_mode.deactivate()        
        self.study_mode.activate()
        
    def show_add_cards_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("add_cards_dialog")\
            (component_manager=self.component_manager).activate()
        # This dialog calls 'create_new_cards' at some point.
        self.database().save()
        review_controller = self.review_controller()
        review_controller.reload_counters()
        if review_controller.card is None:
            review_controller.show_new_question()
        else:
            review_controller.update_status_bar_counters()
        self.stopwatch().unpause()

    def create_new_cards(self, fact_data, card_type, grade, tag_names,
                         check_for_duplicates=True, save=True):

        """Create a new set of sister cards. If the grade is 2 or higher,
        we perform a initial review with that grade and move the cards into
        the long term retention process. For other grades, we treat the card
        as still unseen and keep its grade at -1. This puts the card on equal
        footing with ungraded cards created during the import process. These
        ungraded cards are pulled in at the end of the review process, either
        in the order they were added, on in random order.

        """

        assert grade in [-1, 2, 3, 4, 5] # Use -1 for yet to learn cards.
        assert card_type.is_fact_data_valid(fact_data)
        db = self.database()
        tags = db.get_or_create_tags_with_names(tag_names)
        fact = Fact(fact_data)
        if check_for_duplicates:
            duplicates = db.duplicates_for_fact(fact, card_type)
            if len(duplicates) != 0:
                answer = None
                for duplicate in duplicates:
                    # Duplicates only checks equality of unique keys.
                    if duplicate.data == fact_data:
                        answer = self.main_widget().show_question(\
                            _("Identical card is already in database."),
                            _("&Do not add"), _("&Add anyway"), "")
                        break
                if answer is None:
                    question = \
                        _("There is already data present for this card:\n\n")
                    existing_fact_data = {}
                    for fact_key in card_type.fact_keys():
                        existing_fact_data[fact_key] = ""
                    for duplicate in duplicates:
                        if duplicate.data == fact.data:
                            continue
                        for fact_key in fact_data:
                            if fact_key in duplicate.data and \
                                duplicate[fact_key] not in \
                                existing_fact_data[fact_key]:
                                if len(existing_fact_data[fact_key]) != 0:
                                    existing_fact_data[fact_key] += " / "
                                existing_fact_data[fact_key] +=  \
                                    duplicate[fact_key]
                    for fact_key, fact_key_name in \
                        card_type.fact_keys_and_names:
                        question += _(fact_key_name) + ": " + \
                            existing_fact_data[fact_key] + "\n"
                    answer = self.main_widget().show_question(question,
                    _("&Do not add"), _("&Add anyway"), _("&Merge and edit"))
                if answer == 0:  # Do not add.
                    return
                if answer == 2:  # Merge and edit.
                    db.add_fact(fact)
                    for duplicate in duplicates:
                        for card in db.cards_from_fact(duplicate):
                            tags.update(card.tags)
                    cards = card_type.create_sister_cards(fact)
                    for card in cards:
                        card.tags = tags
                        db.add_card(card)
                    if grade >= 2:
                        self.scheduler().set_initial_grade(cards, grade)
                        for card in cards:
                            db.update_card(card, repetition_only=True)
                    merged_fact_data = copy.copy(fact.data)
                    for duplicate in duplicates:
                        for fact_key in fact_data:
                            if fact_key in duplicate.data and \
                                duplicate[fact_key] not in \
                                    merged_fact_data[fact_key]:
                                merged_fact_data[fact_key] += " / " \
                                    + duplicate[fact_key]
                    self.delete_facts_and_their_cards(duplicates)
                    card = db.cards_from_fact(fact)[0]
                    card.fact.data = merged_fact_data
                    card.tags = tags
                    self.component_manager.current("edit_card_dialog")\
                        (card, component_manager=self.component_manager, 
                         allow_cancel=False).activate()
                    return db.cards_from_fact(fact)
        # Create cards.
        cards = card_type.create_sister_cards(fact)
        db.add_fact(fact)
        for card in cards:
            card.tags = tags
            db.add_card(card)
        if grade >= 2:
            self.scheduler().set_initial_grade(cards, grade)
            for card in cards:
                db.update_card(card, repetition_only=True)
        if save:
            db.save()
        if self.review_controller().learning_ahead == True:
            self.reset_study_mode()
        return cards

    def show_edit_card_dialog(self): 
        self.stopwatch().pause()
        self.flush_sync_server()
        review_controller = self.review_controller()
        # This dialog calls 'edit_card_and_sisters' at some point.
        state = review_controller.state()
        accepted = self.component_manager.current("edit_card_dialog")\
            (review_controller.card, 
             component_manager=self.component_manager).activate()
        if not accepted:
            self.stopwatch().unpause()
            return
        review_controller.reload_counters()
        # Our current card could have disappeared from the database here,
        # e.g. when converting a front-to-back card to a cloze card, which
        # deletes the old cards and their learning history.
        if review_controller.card is None:
            review_controller.show_new_question()
        else:
            review_controller.set_state(state)
            review_controller.card = self.database().card(\
                review_controller.card._id, is_id_internal=True)
            # Our current card could have picked up a forbidden tag.
            if review_controller.card.active == False:
                review_controller.show_new_question()
            review_controller.update_dialog(redraw_all=True)
        self.stopwatch().unpause()

    def _change_card_type(self, fact, old_card_type, new_card_type,
                          correspondence, new_fact_data, warn=True):

        """This is an internal function, used by 'edit_card_and_sisters' and
        'change_card_type'. It should not be called from the outside by
        itself, otherwise the database will not be saved.
        
        Returns -2 for error, -1 for cancel and 0 for success.

        """

        if old_card_type == new_card_type:
            return 0
        db = self.database()
        cards_from_fact = db.cards_from_fact(fact)
        assert cards_from_fact[0].card_type == old_card_type
        if not new_card_type.is_fact_data_valid(new_fact_data):
            self.main_widget().show_error(\
        _("Card data not correctly formatted for conversion.\n\nSkipping ") +\
                "|".join(list(fact.data.values())) + ".\n")
            return -2
        converter = self.component_manager.current\
              ("card_type_converter", used_for=(old_card_type.__class__,
                                                new_card_type.__class__))
        if not converter:
            # Perhaps they have a common ancestor.
            parents_old = old_card_type.id.split("::")
            parents_new = new_card_type.id.split("::")
            if parents_old[0] == parents_new[0]:
                edited_cards = cards_from_fact
                new_fact_view_for = {}
                for index, old_fact_view in enumerate(old_card_type.fact_views):
                    new_fact_view_for[old_fact_view] = \
                        new_card_type.fact_views[index]
                for card in edited_cards:
                    card.card_type = new_card_type
                    card.fact_view = new_fact_view_for[card.fact_view]
                    db.update_card(card)
                return 0
            else:
                if warn:
                    has_history = False
                    for card in cards_from_fact:
                        if card.acq_reps > 0:
                            has_history = True
                            break
                    if has_history:
                        answer = self.main_widget().show_question(\
         _("Can't preserve history when converting between these card types.")\
                 + " " + _("The learning history of the cards will be reset."),
                 _("&OK"), _("&Cancel"), "")
                        if answer == 1:  # Cancel.
                            return -1
                # Go ahead with conversion
                is_currently_asked = \
                   self.review_controller().card in cards_from_fact
                tag_names = cards_from_fact[0].tag_string().split(", ")
                # Don't use progress bars in the next call, as nested
                # progress bars (e.g. from change_card_type) are not
                # supported.
                self.delete_facts_and_their_cards([fact], progress_bar=False)
                new_cards = self.create_new_cards(new_fact_data,
                    new_card_type, grade=-1, tag_names=tag_names)
                # User cancelled in create_new_cards.
                if new_cards is None:
                    return -1
                # We've created a new fact here. Make sure the calling function
                # has the information to reload the fact.
                fact._id = new_cards[0].fact._id
                fact.id = new_cards[0].fact.id
                if is_currently_asked:
                    self.review_controller().card = new_cards[0]
                return 0
        else:
            for card in cards_from_fact:
                card.card_type = new_card_type
                card.fact = fact
            new_cards, edited_cards, deleted_cards = converter.convert(\
                cards_from_fact, old_card_type, new_card_type, correspondence)
            if warn and len(deleted_cards) != 0:
                answer = self.main_widget().show_question(\
          _("This will delete cards and their history.") + " " +\
          _("Are you sure you want to do this,") + " " +\
          _("and not just deactivate cards in the 'Activate cards' dialog?"),
                 _("&Proceed and delete"), _("&Cancel"), "")
                if answer == 1:  # Cancel.
                    return -1
            for card in deleted_cards:
                if self.review_controller().card == card:
                    self.review_controller().card = None
                self.scheduler().remove_from_queue_if_present(card)
                db.delete_card(card)
            for card in new_cards:
                card.tags = cards_from_fact[0].tags
                db.add_card(card)
            for card in edited_cards:
                db.update_card(card)
            if new_cards and self.review_controller().learning_ahead:
                self.reset_study_mode()
            return 0

    def edit_card_and_sisters(self, card, new_fact_data, new_card_type,
            new_tag_names, correspondence):
        db = self.database()
        sch = self.scheduler()
        assert new_card_type.is_fact_data_valid(new_fact_data)
        # Determine the current tags in use for the sister cards. This
        # needs to be done before e.g. editing a cloze card creates new
        # cards which are as yet untagged.
        fact = db.fact(card.fact._id, is_id_internal=True)
        current_sister_cards = self.database().cards_from_fact(fact)
        current_tag_strings = set([sister_card.tag_string() \
            for sister_card in current_sister_cards])        
        # Change the card type if needed. This does not take into account
        # changes to fact yet, which will come just afterwards.
        result = self._change_card_type(card.fact, card.card_type,
            new_card_type, correspondence, new_fact_data)
        if result in [-2, -1]:  # Error, aborted.
            return result
        # When there was no card type conversion possible, the cards had to
        # be recreated from the new fact data. In that case, it is needed to
        # reload the fact from the database.
        fact = db.fact(card.fact._id, is_id_internal=True)
        # Update fact and create, delete and update cards.
        new_cards, edited_cards, deleted_cards = \
            new_card_type.edit_fact(fact, new_fact_data)
        fact.data = new_fact_data
        db.update_fact(fact)
        for deleted_card in deleted_cards:
            if self.review_controller().card == deleted_card:
                self.review_controller().card = None
            sch.remove_from_queue_if_present(deleted_card)
            db.delete_card(deleted_card)
        for new_card in new_cards:
            db.add_card(new_card)
        for edited_card in edited_cards:
            db.update_card(edited_card)
        if new_cards and self.review_controller().learning_ahead == True:
            self.reset_study_mode()
        # Apply new tags and modification time to cards and save them back to
        # the database. Note that this makes sure there is an EDITED_CARD log
        # entry for each sister card, which is needed when syncing with a
        # partner that does not have the concept of facts.
        tag_for_current_card_only = False
        if len(current_tag_strings) > 1:
            tag_for_current_card_only = bool(self.main_widget().show_question(
_("This card has different tags than its sister cards. Update tags for current card only or for all sister cards?"),
            _("Current card only"), _("All sister cards"), "") == 0)
        old_tags = set()
        tags = db.get_or_create_tags_with_names(new_tag_names)
        modification_time = int(time.time())
        for sister_card in self.database().cards_from_fact(fact):
            sister_card.modification_time = modification_time
            if sister_card == card or not tag_for_current_card_only:
                old_tags = old_tags.union(sister_card.tags)
                sister_card.tags = tags
            db.update_card(sister_card)
        for tag in old_tags:
            db.delete_tag_if_unused(tag)
        db.save()
        return 0

    def change_card_type(self, facts, old_card_type, new_card_type,
                         correspondence):

        """Note: all facts should have the same card type."""

        db = self.database()
        warn = True
        w = self.main_widget()
        w.set_progress_text(_("Converting cards..."))
        w.set_progress_range(len(facts))
        w.set_progress_update_interval(len(facts)/50)
        for fact in facts:
            if correspondence:
                new_fact_data = {}
                for old_fact_key, new_fact_key in correspondence.items():
                    if old_fact_key in fact:
                        new_fact_data[new_fact_key] = fact[old_fact_key]
                if new_card_type.is_fact_data_valid(new_fact_data):
                    fact.data = new_fact_data
            else:
                new_fact_data = copy.copy(fact.data)  
            result = self._change_card_type(fact, old_card_type,
                    new_card_type, correspondence, new_fact_data, warn)
            if result == -1:  # Cancel.
                w.close_progress()
                return                    
            if correspondence and result != -2:  # Error. 
                db.update_fact(fact)              
            warn = False
            w.increase_progress(1)
        db.save()
        w.close_progress()

    def star_current_card(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        if self.config()["star_help_shown"] == False:
            self.main_widget().show_information(\
_("This will add a tag 'Starred' to the current card, so that you can find it back easily, e.g. to edit it on a desktop."))
            self.config()["star_help_shown"] = True        
        db = self.database()
        review_controller = self.review_controller()
        tag = db.get_or_create_tag_with_name(_("Starred"))
        _sister_card_ids = [card._id for card in \
            self.database().cards_from_fact(review_controller.card.fact)]
        db.add_tag_to_cards_with_internal_ids(tag, _sister_card_ids)
        review_controller.card = \
            db.card(review_controller.card._id, is_id_internal=True)
        review_controller.update_dialog(redraw_all=True)
        self.stopwatch().unpause()

    def delete_current_card(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        db = self.database()
        review_controller = self.review_controller()
        fact = review_controller.card.fact
        no_of_cards = len(db.cards_from_fact(fact))
        if no_of_cards == 1:
            question = _("Delete this card?")
        elif no_of_cards == 2:
            question = _("Delete this card and 1 sister card?") + " "  +\
                      _("Are you sure you want to do this,") + " " +\
          _("and not just deactivate cards in the 'Activate cards' dialog?")
        else:
            question = _("Delete this card and") + " " + str(no_of_cards - 1) \
                       + " " + _("sister cards?") + " " +\
                       _("Are you sure you want to do this,") + " " +\
          _("and not just deactivate cards in the 'Activate cards' dialog?")
        answer = self.main_widget().show_question(question, _("&Cancel"),
                                          _("&Delete"), "")
        if answer == 0:  # Cancel.
            self.stopwatch().unpause()
            return
        self.delete_facts_and_their_cards([fact])
        review_controller.reload_counters()
        review_controller.show_new_question()
        self.stopwatch().unpause()

    def delete_facts_and_their_cards(self, facts, progress_bar=True):
        assert len(facts) == len([fact.id for fact in facts])
        db = self.database()
        w = self.main_widget()
        if progress_bar:
            w.set_progress_text(_("Deleting cards..."))
            w.set_progress_range(len(facts))
            w.set_progress_update_interval(50)
        for fact in facts:
            for card in db.cards_from_fact(fact):
                self.scheduler().remove_from_queue_if_present(card)
                db.delete_card(card, check_for_unused_tags=False)
            db.delete_fact(fact)
            if progress_bar:
                w.increase_progress(1)
        tags = db.tags()
        if progress_bar:
            w.set_progress_text(_("Checking for unused tags..."))
            w.set_progress_range(len(tags))
        tags = db.tags()
        for tag in tags:
            db.delete_tag_if_unused(tag)
            if progress_bar:
                w.increase_progress(1)
        db.save()
        if progress_bar:
            w.close_progress()

    def clone_card_type(self, card_type, clone_name):
        from mnemosyne.libmnemosyne.utils import mangle
        clone_id = card_type.id + "::" + clone_name
        if clone_id in [card_t.id for card_t in self.card_types()]:
            self.main_widget().show_error(_("Card type name already exists."))
            return None
        card_type_class = type(mangle(clone_name), (card_type.__class__, ),
            {"name": clone_name, "id": clone_id})
        cloned_card_type = card_type_class(self.component_manager)
        cloned_card_type.fact_views = []
        for fact_view in card_type.fact_views:
            cloned_fact_view = copy.copy(fact_view)
            cloned_fact_view.id = clone_id + "." + fact_view.id.rsplit(".", 1)[1]
            cloned_card_type.fact_views.append(cloned_fact_view)
            self.database().add_fact_view(cloned_fact_view)
        self.database().add_card_type(cloned_card_type)
        self.config().clone_card_type_properties(card_type, cloned_card_type)
        self.database().save()
        return cloned_card_type

    def delete_card_type(self, card_type):
        if not self.database().is_user_card_type(card_type) or \
            self.database().is_in_use(card_type):
            self.main_widget().show_error(\
    _("Card type %s is in use or is a system card type, cannot delete it.") \
    % (card_type.name, ))
            return
        if self.database().has_clones(card_type):
            self.main_widget().show_error(\
    _("Card type %s has clones, cannot delete it.") \
    % (card_type.name, ))
            return
        fact_views = card_type.fact_views
        self.config().delete_card_type_properties(card_type)
        self.database().delete_card_type(card_type)
        # Correct ordering for the sync protocol is deleting the fact views
        # last.
        for fact_view in fact_views:
            self.database().delete_fact_view(fact_view)
        self.database().save()

    def rename_card_type(self, card_type, new_name):
        if not self.database().is_user_card_type(card_type):
            self.main_widget().show_error(\
            _("Cannot rename a system card type."))
            return
        for card_type_ in self.card_types():
            if card_type_.name == new_name:
                self.main_widget().show_error(\
                _("This card type name is already in use."))
                return
        card_type.name = new_name
        self.database().update_card_type(card_type)
        self.database().save()

    single_database_help = \
_("It is recommended to put all your cards in a single database. Using tags to determine which cards to study is much more convenient than having to load and unload several databases.")

    def show_new_file_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        if self.config()["single_database_help_shown"] == False:
            self.main_widget().show_information(self.single_database_help)
            self.config()["single_database_help_shown"] = True
        db = self.database()
        suffix = db.suffix
        filename = self.main_widget().get_filename_to_save(\
            path=self.config().data_dir, filter=_("Mnemosyne databases") + \
            " (*%s)" % suffix, caption=_("New"))
        if not filename:
            self.stopwatch().unpause()
            return
        if not filename.endswith(suffix):
            filename += suffix
        self.main_widget().set_progress_text(_("Creating new database..."))
        db.backup()
        db.unload()
        # Confirmation on overwrite has happened in the file dialog code.
        if os.path.exists(filename + "_media"):
            import shutil
            shutil.rmtree(filename + "_media")
        db.new(filename)
        self.main_widget().close_progress()
        db.load(self.config()["last_database"])
        self.log().loaded_database()
        self.reset_study_mode()
        self.review_controller().update_dialog()
        self.update_title()
        self.stopwatch().unpause()

    def show_open_file_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        db = self.database()
        data_dir = self.config().data_dir
        old_path = expand_path(self.config()["last_database"], data_dir)
        filename = self.main_widget().get_filename_to_open(path=old_path,
            filter=_("Mnemosyne databases") + " (*%s)" % db.suffix)
        if not filename:
            self.stopwatch().unpause()
            return
        if filename.endswith(".cards"):
            self.stopwatch().unpause()
            self.main_widget().show_information(\
_("'*.cards' files are not separate databases, but need to be imported in your current database through 'File - Import'."))
            return
        if filename.endswith("config.db"):
            self.stopwatch().unpause()
            self.main_widget().show_information(\
                _("The configuration database is not used to store cards."))
            return
        if os.path.normpath(filename).startswith(\
            os.path.normpath(os.path.join(data_dir, "backups"))):
            result = self.main_widget().show_question(\
                _("Do you want to replace your current database with one restored from this backup?\nNote that this will result in conflicts during the next sync, which need to be resolved by a full sync."),
                _("Yes"), _("No"), "")
            if result == 0:  # Yes.
                # Note that we don't save the current database first in this
                # case, as the user wants to throw it away. This mainly
                # prohibits dumping to the science log.
                db.restore(filename)
                self.reset_study_mode()
                self.update_title()
            self.stopwatch().unpause()
            return
        if self.database().is_loaded():
            try:
                self.log().saved_database()
                db.backup()
                db.unload()
            except RuntimeError as error:
                self.main_widget().show_error(str(error))
                self.stopwatch().unpause()
                return
        try:
            db.load(filename)
            self.log().loaded_database()
            self.log().future_schedule()
        except Exception as error:
            self.main_widget().show_error(str(error))
            db.abandon()
            db.load(old_path)
            self.stopwatch().unpause()
            return
        self.reset_study_mode()
        self.update_title()
        self.stopwatch().unpause()

    def save_file(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        if self.config()["save_database_help_shown"] == False:
            self.main_widget().show_information(\
_("Your database will be autosaved before exiting. Also, it is saved every couple of repetitions, as set in the configuration options."))
            self.config()["save_database_help_shown"] = True
        try:
            self.database().save()
            self.log().saved_database()
            self.main_widget().show_information(_("Database saved."))
        except RuntimeError as error:
            self.main_widget().show_error(str(error))
        self.stopwatch().unpause()

    def show_save_file_as_dialog(self):
        self.stopwatch().pause()
        if self.config()["single_database_help_shown"] == False:
            self.main_widget().show_information(_(self.single_database_help))
            self.config()["single_database_help_shown"] = True
        self.flush_sync_server()
        suffix = self.database().suffix
        old_path = expand_path(self.config()["last_database"], self.config().data_dir)
        old_media_dir = self.database().media_dir()
        filename = self.main_widget().get_filename_to_save(path=old_path,
            filter=_("Mnemosyne databases") + " (*%s)" % suffix)
        if not filename:
            self.stopwatch().unpause()
            return
        if filename.endswith("config.db"):
            self.main_widget().show_information(\
_("The configuration database cannot be used to store cards."))
            self.stopwatch().unpause()
            return          
        if not filename.endswith(suffix):
            filename += suffix
        try:
            self.database().save(filename)
            new_media_dir = self.database().media_dir()
            if old_media_dir == new_media_dir:
                return
            if os.path.exists(new_media_dir):
                shutil.rmtree(new_media_dir)
            shutil.copytree(old_media_dir, new_media_dir)
            self.log().saved_database()
        except RuntimeError as error:
            self.main_widget().show_error(str(error))
            self.stopwatch().unpause()
            return
        self.review_controller().update_dialog()
        self.update_title()
        self.stopwatch().unpause()

    def show_compact_database_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("compact_database_dialog")\
            (component_manager=self.component_manager).activate()
        self.review_controller().reset_but_try_to_keep_current_card()
        self.stopwatch().unpause()       
            
    def show_insert_img_dialog(self, filter):

        """Show a file dialog filtered on the supported filetypes, get a
        filename, massage it, and return it to the widget to be inserted.
        There is more media file logic inside the database code too, as the
        user could also just type in the html tags as opposed to passing
        through the file selector here. The reason we don't do all the
        operations in the database code, is that we want to display a nice
        short relative path back in the edit field.

        """

        from mnemosyne.libmnemosyne.utils import copy_file_to_dir
        data_dir, media_dir = \
            self.config().data_dir, self.database().media_dir()
        path = expand_path(self.config()["import_img_dir"], data_dir)
        filter = _("Image files") + " " + filter
        filename = self.main_widget().get_filename_to_open(\
            path, filter, _("Insert image"))
        if not filename:
            return ""
        else:
            self.config()["import_img_dir"] = contract_path(\
                os.path.dirname(filename), data_dir)
            filename = copy_file_to_dir(filename, media_dir)
            return contract_path(filename, media_dir)

    def show_insert_sound_dialog(self, filter):
        from mnemosyne.libmnemosyne.utils import copy_file_to_dir
        data_dir, media_dir = self.config().data_dir, self.database().media_dir()
        path = expand_path(self.config()["import_sound_dir"], data_dir)
        filter = _("Sound files") + " " + filter
        filename = self.main_widget().get_filename_to_open(\
            path, filter, _("Insert sound"))
        if not filename:
            return ""
        else:
            self.config()["import_sound_dir"] = contract_path(\
                os.path.dirname(filename), data_dir)
            filename = copy_file_to_dir(filename, media_dir)
            return filename

    def show_insert_video_dialog(self, filter):
        from mnemosyne.libmnemosyne.utils import copy_file_to_dir
        data_dir, media_dir = self.config().data_dir, self.database().media_dir()
        path = expand_path(self.config()["import_video_dir"], data_dir)
        filter = _("Video files") + " " + filter
        filename = self.main_widget().get_filename_to_open(\
            path, filter, _("Insert video"))
        if not filename:
            return ""
        else:
            self.config()["import_video_dir"] = contract_path(\
                os.path.dirname(filename), data_dir)
            filename = copy_file_to_dir(filename, media_dir)
            return filename

    def show_insert_flash_dialog(self, filter):
        from mnemosyne.libmnemosyne.utils import copy_file_to_dir
        data_dir, media_dir = self.config().data_dir, self.database().media_dir()
        path = expand_path(self.config()["import_flash_dir"], data_dir)
        filter = _("Flash files") + " " + filter
        filename = self.main_widget().get_filename_to_open(\
            path, filter, _("Insert Flash"))
        if not filename:
            return ""
        else:
            self.config()["import_flash_dir"] = contract_path(\
                os.path.dirname(filename), data_dir)
            filename = copy_file_to_dir(filename, media_dir)
            return filename

    def show_browse_cards_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        review_controller = self.review_controller()
        self.component_manager.current("browse_cards_dialog")\
            (component_manager=self.component_manager).activate()
        review_controller.reset_but_try_to_keep_current_card()
        review_controller.update_dialog(redraw_all=True)
        self.stopwatch().unpause()

    def show_activate_cards_dialog(self):
        self.show_activate_cards_dialog_pre()
        self.show_activate_cards_dialog_post()
         
    def show_activate_cards_dialog_pre(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("activate_cards_dialog")\
            (component_manager=self.component_manager).activate()
        
    def show_activate_cards_dialog_post(self):       
        review_controller = self.review_controller()
        review_controller.reset_but_try_to_keep_current_card()
        review_controller.update_status_bar_counters()
        self.update_title()
        self.stopwatch().unpause()

    def find_duplicates(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        if self.config()["find_duplicates_help_shown"] == False:
            self.main_widget().show_information(\
_("This will tag all the cards in a given card type which have the same question. That way you can reformulate them to make the answer unambiguous. Note that this will not tag duplicates in different card types, e.g. card types for 'French' and 'Spanish'."))
            self.config()["find_duplicates_help_shown"] = True
        self.database().tag_all_duplicates()
        review_controller = self.review_controller()
        review_controller.reset_but_try_to_keep_current_card()
        review_controller.update_status_bar_counters()
        self.stopwatch().unpause()

    def show_manage_plugins_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("manage_plugins_dialog")\
            (component_manager=self.component_manager).activate()
        self.review_controller().update_dialog(redraw_all=True)
        self.stopwatch().unpause()

    def install_plugin(self):
        filename = self.main_widget().get_filename_to_open(\
            self.config()["import_plugin_dir"],
            _("Plugins") + " " + "(*.plugin)", _("Install plugin"))
        if not filename:
            return ""
        self.config()["import_plugin_dir"] = os.path.dirname(filename)
        plugin_dir = os.path.join(self.config().data_dir, "plugins")
        import zipfile
        plugin_file = zipfile.ZipFile(filename, "r")
        # Filter out safety risks.
        filenames = [filename for filename in plugin_file.namelist() \
            if not filename.startswith("/") and not ".." in filename]
        # Find actual plugin.
        plugin_file.extractall(plugin_dir, filenames)
        import re
        re_plugin = re.compile(r""".*class (.+?)\(Plugin""",
            re.DOTALL | re.IGNORECASE)
        plugin_filename, plugin_class_name = None, None
        for filename in filenames:
            if filename.endswith(".py"):
                text = open(os.path.join(plugin_dir, filename), "r").read()
                match = re_plugin.match(text)
                if match is not None:
                    plugin_filename = filename
                    plugin_class_name = match.group(1)
                    break
        if plugin_class_name is None:
            self.main_widget().show_error(_("No plugin found!"))
            return
        # Write manifest to allow uninstalling.
        manifest = open(os.path.join(plugin_dir,
            plugin_class_name + ".manifest"), "w")
        for filename in filenames:
            if not os.path.isdir(os.path.join(plugin_dir, filename)):
                print(filename, file=manifest)
        # Make sure we don't register a plugin twice.
        for plugin in self.plugins():
            if plugin.__class__.__name__ == plugin_class_name:
                return
        # Register plugin.
        try:
            module_name = plugin_filename[:-3]
            # Schedule module for reloading. Needed to catch the case of
            # deleting a plugin and then immediately reinstalling it.
            if module_name in sys.modules:
                del sys.modules[module_name]
            __import__(module_name)
        except Exception as e:
            print(e)
            from mnemosyne.libmnemosyne.utils import traceback_string
            msg = _("Error when running plugin:") \
                + "\n" + traceback_string()
            self.main_widget().show_error(msg)
            for plugin in self.plugins():
                if plugin.__class__.__name__ == plugin_class_name:
                    self.component_manager.unregister(plugin)

    def delete_plugin(self, plugin):
        plugin.deactivate()
        self.component_manager.unregister(plugin)
        plugin_dir = os.path.join(self.config().data_dir, "plugins")
        manifest_filename = os.path.join(plugin_dir,
            plugin.__class__.__name__ + ".manifest")
        manifest = open(manifest_filename, "r")
        plugin_dir = os.path.join(self.config().data_dir, "plugins")
        for filename in manifest:
            filename = os.path.join(plugin_dir, filename.rstrip())
            os.remove(filename)
            if filename.endswith(".py") and os.path.exists(filename + "c"):
                os.remove(filename + "c")
        manifest.close()
        del plugin
        os.remove(manifest_filename)
        remove_empty_dirs_in(plugin_dir)

    def show_manage_card_types_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("manage_card_types_dialog")\
            (component_manager=self.component_manager).activate()
        self.stopwatch().unpause()

    def show_statistics_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("statistics_dialog")\
            (component_manager=self.component_manager).activate()
        self.stopwatch().unpause()

    def show_configuration_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("configuration_dialog")\
            (component_manager=self.component_manager).activate()
        self.config().save()
        self.review_controller().reset_but_try_to_keep_current_card()
        self.review_controller().update_dialog(redraw_all=True)
        self.stopwatch().unpause()

    def show_import_file_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("import_dialog")\
            (component_manager=self.component_manager).activate()
        self.database().save()
        self.log().saved_database()
        review_controller = self.review_controller()
        review_controller.reload_counters()
        # Importing can edit the current card.
        self.review_controller().reset_but_try_to_keep_current_card()
        self.review_controller().update_dialog(redraw_all=True)
        self.stopwatch().unpause()

    def show_export_file_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("export_dialog")\
            (component_manager=self.component_manager).activate()
        self.stopwatch().unpause()

    def show_export_metadata_dialog(self, metadata=None, read_only=False):
        self.stopwatch().pause()
        self.flush_sync_server()
        dialog = self.component_manager.current("export_metadata_dialog")\
            (component_manager=self.component_manager)
        if metadata:
            dialog.set_values(metadata)
        if read_only:
            dialog.set_read_only()
        dialog.activate()
        self.stopwatch().unpause()
        return dialog.values()

    def show_sync_dialog(self):
        self.show_sync_dialog_pre()
        self.show_sync_dialog_post()
        
    def show_sync_dialog_pre(self): 
        self.stopwatch().pause()
        self.flush_sync_server()
        self.database().save()
        self.component_manager.current("sync_dialog")\
            (component_manager=self.component_manager).activate()
        
    def show_sync_dialog_post(self):
        self.database().save()
        self.log().saved_database()
        self.review_controller().reset_but_try_to_keep_current_card()
        self.review_controller().update_dialog(redraw_all=True)
        self.stopwatch().unpause()

    def sync(self, server, port, username, password, ui=None):
        if ui is None:
            ui = self.main_widget()
        from openSM2sync.client import Client
        client = Client(self.config().machine_id(), self.database(), ui)
        client.program_name = "Mnemosyne"
        import mnemosyne.version
        client.program_version = mnemosyne.version.version
        client.capabilities = "mnemosyne_dynamic_cards"
        client.check_for_edited_local_media_files = \
            self.config()["check_for_edited_local_media_files"]
        client.interested_in_old_reps = \
            self.config()["interested_in_old_reps"]
        client.store_pregenerated_data = \
            self.database().store_pregenerated_data
        client.do_backup = self.config()["backup_before_sync"]
        client.upload_science_logs = self.config()["upload_science_logs"]
        try:         
            client.sync(server, port, username, password)
        finally:
            client.database.release_connection()

    def show_download_source_dialog(self):

        """The following code is here to be able to enforce the AGPL licence.

        If you run Mnemosyne as a service over the network, you need to provide
        users the option to download your modified version of libmnemosyne and
        the Mnemosyne HTML server.

        The recommended way to do this is to provide a link at the bottom of
        the webpage saying "Flash cards by Mnemosyne", with "Mnemosyne" a link
        taking you to a page with download instructions for the copy of
        Mnemosyne you are using.

        Even if you are using an unmodified version of Mnemosyne, you should
        still host a copy of that source code on your site, in order to set an
        example for people who do modify the source.

        """

        self.stopwatch().pause()
        self.flush_sync_server()
        self.main_widget().show_information(\
            _("For instructions on how to download Mnemosyne's source,") + \
            " " + _("go to http://www.mnemosyne-proj.org"))
        self.stopwatch().unpause()

    def show_getting_started_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("getting_started_dialog")\
            (component_manager=self.component_manager).activate()
        self.stopwatch().unpause()

    def show_tip_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("tip_dialog")\
            (component_manager=self.component_manager).activate()
        self.stopwatch().unpause()

    def show_about_dialog(self):
        self.stopwatch().pause()
        self.flush_sync_server()
        self.component_manager.current("about_dialog")\
            (component_manager=self.component_manager).activate()
        self.stopwatch().unpause()