Exemple #1
0
    def _process_media(self, fact):

        """Copy the media files to the media directory and update the media
        table. We don't keep track of which facts use which media and delete
        a media file if it's no longer in use. The reason for this is that some
        people use the media directory as their only location to store their
        media files, and also use these files for other purposes.
        
        When we are applying log entries during sync, we should not generate
        extra log entries, this will be taken care of by the syncing
        algorithm. (Not all 'added_media' events originated here, they are
        also generated by the latex subsystem, or by checking for files which
        were modified outside of Mnemosyne.

        """

        for match in re_src.finditer("".join(fact.data.values())):
            filename = match.group(1)
            # If needed, copy file to the media dir. Normally this happens when
            # the user clicks 'Add image' e.g., but he could have typed in the
            # full path directly.
            if os.path.isabs(filename):
                filename = copy_file_to_dir(filename, self.mediadir())
            else:  # We always store Unix paths internally.
                filename = filename.replace("\\", "/")
            for key, value in fact.data.iteritems():
                fact.data[key] = value.replace(match.group(1), filename)
                self.con.execute("""update data_for_fact set value=? where
                    _fact_id=? and key=?""", (fact.data[key], fact._id, key))
            if self.con.execute("select count() from media where filename=?",
                                (filename, )).fetchone()[0] == 0:
                self.con.execute("""insert into media(filename, _hash)
                    values(?,?)""", (filename, self._media_hash(filename)))
                if not self.syncing:
                    self.log().added_media(filename)
    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)
Exemple #3
0
    def _process_media(self, fact):

        """Copy the media files to the media directory and edit the media
        table. We don't keep track of which facts use which media and delete
        a media file if it's no longer in use. The reason for this is that some
        people use the media directory as their only location to store their
        media files, and also use these files for other purposes.

        Note that not all 'added_media_file' events originated here, they are
        also generated by the latex subsystem, or by checking for files which
        were modified outside of Mnemosyne.

        """

        for match in re_src.finditer("".join(fact.data.values())):
            filename = match.group(2)
            if filename.startswith("http:"):
                continue
            if len(filename) > 200:
                self.main_widget().show_information(
                    _("Media filename rather long. This could cause problems using this file on a different OS.")
                )
            if "#" in filename:
                self.main_widget().show_information(
                    _("Filename contains '#', which could cause problems on some operating systems.")
                )
            if not path_exists(filename) and not path_exists(expand_path(filename, self.media_dir())):
                self.main_widget().show_error(_("Missing media file!") + "\n\n" + filename)
                for fact_key, value in fact.data.iteritems():
                    fact.data[fact_key] = fact.data[fact_key].replace(
                        match.group(), 'src_missing="%s"' % match.group(2)
                    )
                continue
            # If needed, copy file to the media dir. Normally this happens when
            # the user clicks 'Add image' e.g., but he could have typed in the
            # full path directly.
            if os.path.isabs(filename):
                filename = copy_file_to_dir(filename, self.media_dir())
            else:  # We always store Unix paths internally.
                filename = filename.replace("\\", "/")
            for fact_key, value in fact.data.iteritems():
                fact.data[fact_key] = value.replace(match.group(2), filename)
                self.con.execute(
                    """update data_for_fact set value=? where
                    _fact_id=? and key=?""",
                    (fact.data[fact_key], fact._id, fact_key),
                )
            if self.con.execute("select count() from media where filename=?", (filename,)).fetchone()[0] == 0:
                self.con.execute(
                    """insert into media(filename, _hash)
                    values(?,?)""",
                    (filename, self._media_hash(filename)),
                )
                # When we are applying log entries during sync or import, the
                # side effects of e.g. ADDED_FACT events should not generate
                # additional ADDED_MEDIA_FILE events at the remote partner, so
                # we disable the logging of these side effects in that case.
                if not self.syncing and not self.importing:
                    self.log().added_media_file(filename)
Exemple #4
0
    def insert_video(self, filter):
        from mnemosyne.libmnemosyne.utils import copy_file_to_dir

        basedir, mediadir = self.config().basedir, self.database().mediadir()
        path = expand_path(self.config()["import_video_dir"], basedir)
        filter = _("Video files") + " " + filter
        filename = self.main_widget().open_file_dialog(path, filter, _("Insert video"))
        if not filename:
            return ""
        else:
            self.config()["import_video_dir"] = contract_path(os.path.dirname(filename), basedir)
            filename = copy_file_to_dir(filename, mediadir)
            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
Exemple #6
0
 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
Exemple #7
0
 def _process_media(self, fact):
     mediadir = self.config().mediadir()
     # Determine new media files for this fact. Copy them to the media dir
     # if needed. (The user could have typed in the full path directly
     # withouh going through the add_img or add_sound callback.)
     matches = re_src.finditer("".join(fact.data.values()))
     if not matches:
         return
     new_files = set()
     for match in matches:
         filename = match.group(1)
         if os.path.isabs(filename):
             filename = copy_file_to_dir(filename, mediadir)
             for key, value in fact.data.iteritems():
                 fact.data[key] = value.replace(match.group(1), filename)
                 self.con.execute("""update data_for_fact set value=? 
                     where _fact_id=? and key=?""", \
                     (fact.data[key], fact._id, key))
         new_files.add(filename)       
     # Determine old media files for this fact.
     old_files = set((cursor["filename"] for cursor in self.con.execute(\
         "select filename from media where _fact_id=?", (fact._id, ))))
     # Update the media table and log additions or deletions. We record
     # the modification date so that we can detect if media files have
     # been modified outside of Mnemosyne. (Although less robust,
     # modifaction dates are faster to lookup then calculating a hash,
     # especially on mobile devices.
     for filename in old_files - new_files:
         self.con.execute("""delete from media where filename=?
             and _fact_id=?""", (filename, fact._id))
         self.log().deleted_media(filename, fact)
         # Delete the media file if it's not used by other facts.
         if self.con.execute("select count() from media where filename=?",
                             (filename, )).fetchone()[0] == 0:
             os.remove(os.path.join(mediadir, filename))
     for filename in new_files - old_files:
         self.con.execute("""insert into media(filename, _fact_id,
             last_modified) values(?,?,?)""", (filename, fact._id,
             int(os.path.getmtime(os.path.join(mediadir, filename)))))
         self.log().added_media(filename, fact)
Exemple #8
0
    def insert_img(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 fileselector here.

        """

        from mnemosyne.libmnemosyne.utils import copy_file_to_dir

        basedir, mediadir = self.config().basedir, self.database().mediadir()
        path = expand_path(self.config()["import_img_dir"], basedir)
        filter = _("Image files") + " " + filter
        filename = self.main_widget().open_file_dialog(path, filter, _("Insert image"))
        if not filename:
            return ""
        else:
            self.config()["import_img_dir"] = contract_path(os.path.dirname(filename), basedir)
            filename = copy_file_to_dir(filename, mediadir)
            return contract_path(filename, mediadir)