def search_bar_old(self, dir, query):
   files = list_all_files(dir)
   
   progress = QProgressDialog("", "Abort", 0, len(files), self)
   progress.setWindowTitle("Searching...")
   progress.setWindowModality(Qt.Qt.WindowModal)
   progress.setValue(0)
   
   width = self.width()
   height = self.height()
   x = self.x()
   y = self.y()
   
   matches = []
   
   if not self.ui.chkRegEx.isChecked():
     query = re.escape(query)
   
   query_re = re.compile(query, re.IGNORECASE | re.DOTALL | re.UNICODE)
   
   for i, file in enumerate(files):
     
     if progress.wasCanceled():
       break
     
     if i % 500 == 0:
       progress.setLabelText(file)
       
       # Re-center the dialog.
       progress_w = progress.geometry().width()
       progress_h = progress.geometry().height()
       
       new_x = x + ((width - progress_w) / 2)
       new_y = y + ((height - progress_h) / 2)
       
       progress.move(new_x, new_y)
     
     if os.path.splitext(file)[1] == ".txt":
       text = load_text(file)
       if not query_re.search(text) == None:
         matches.append(file)
     
     progress.setValue(i + 1)
   
   progress.close()
   
   return matches
def import_umdimage(src, dst, convert_png=True, propogate=True, parent=None):
    src = os.path.abspath(src)
    dst = os.path.abspath(dst)
    if os.path.normcase(src) == os.path.normcase(dst):
        raise ValueError(
            "Cannot import %s. Source and destination directories are the same."
            % src)

    answer = QtGui.QMessageBox.question(
        parent,
        "Import Directory",
        "Importing directory:\n\n" + src + "\n\n" + "into directory:\n\n" +
        dst + "\n\n" + "Any affected files will be backed up. Proceed?",
        buttons=QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
        defaultButton=QtGui.QMessageBox.No)

    if answer == QtGui.QMessageBox.No:
        return

    progress = QProgressDialog("Finding files...", "Cancel", 0, 1, parent)
    progress.setWindowTitle("Importing...")
    progress.setWindowModality(Qt.Qt.WindowModal)
    progress.setValue(0)
    progress.setAutoClose(False)
    progress.setMinimumDuration(0)

    if parent:
        width = parent.width()
        height = parent.height()
        x = parent.x()
        y = parent.y()
    else:
        width = 1920
        height = 1080
        x = 0
        y = 0

    progress.setMaximum(0)
    progress.setValue(0)

    # The raw list of files we're importing.
    files = []

    # A list of lists, including all dupes of the files being imported, too.
    affected_files = []
    file_count = 0

    dupe_base = "umdimage"
    tmp = tempfile.mkdtemp(prefix="sdse-")

    seen_groups = []

    count = 0
    last_update = time.time()

    for file in list_all_files(src):
        if progress.wasCanceled():
            break

        # Strip our base directory, so we have just a relative file list.
        file = os.path.normpath(os.path.normcase(file[len(src) + 1:]))
        files.append(file)

        count += 1
        if time.time() - last_update > MIN_INTERVAL or count % 25 == 0:
            last_update = time.time()
            progress.setLabelText("Finding files...\n" + file)
            # progress.setValue(count)
            progress.setValue(progress.value() ^ 1)

            # Re-center the dialog.
            progress_w = progress.geometry().width()
            progress_h = progress.geometry().height()

            new_x = x + ((width - progress_w) / 2)
            new_y = y + ((height - progress_h) / 2)

            progress.move(new_x, new_y)

        affected_files.append([])

        if os.path.splitext(
                file)[1] == ".png" and convert_png and file not in SKIP_CONV:
            file = os.path.splitext(file)[0] + ".gim"

        if propogate:
            file_group = _DUPE_DB.group_from_file(os.path.join(
                dupe_base, file))
        else:
            file_group = None

        if file_group in seen_groups:
            continue

        # If there are no dupes, just add this file.
        if file_group == None:
            affected_files[-1].append(file)
            file_count += 1
            continue

        seen_groups.append(file_group)
        for dupe in _DUPE_DB.files_in_group(file_group):
            # Minus the "umdimage" part
            dupe = dupe[len(dupe_base) + 1:]
            affected_files[-1].append(dupe)
            file_count += 1

    progress.setValue(0)
    progress.setMaximum(file_count)

    # Make a backup first.
    backup_dir = None
    count = 0
    for file_set in affected_files:
        if progress.wasCanceled():
            break
        for file in file_set:
            if progress.wasCanceled():
                break
            count += 1
            if time.time() - last_update > MIN_INTERVAL or count % 25 == 0:
                last_update = time.time()
                progress.setLabelText("Backing up...\n" + file)
                progress.setValue(count)

                # Re-center the dialog.
                progress_w = progress.geometry().width()
                progress_h = progress.geometry().height()

                new_x = x + ((width - progress_w) / 2)
                new_y = y + ((height - progress_h) / 2)

                progress.move(new_x, new_y)

            # It's perfectly possible we want to import some files that
            # don't already exist. Such as when importing a directory
            # with added lines.
            if not os.path.isfile(os.path.join(dst, file)):
                continue

            backup_dir = backup_files(dst, [file],
                                      suffix="_IMPORT",
                                      backup_dir=backup_dir)

    progress.setValue(0)

    # And now do our importing.
    import_all_new = False
    skip_all_new = False
    count = 0
    for index, src_file in enumerate(files):
        if progress.wasCanceled():
            break

        if os.path.splitext(src_file)[
                1] == ".png" and convert_png and src_file not in SKIP_CONV:
            tmp_src_file = os.path.join(tmp, os.path.basename(src_file))
            tmp_src_file = os.path.splitext(tmp_src_file)[0] + ".gim"
            quantize = QuantizeType.auto
            for regex, q in FORCE_QUANTIZE:
                if not regex.search(src_file) == None:
                    quantize = q
                    break
            _CONV.png_to_gim(os.path.join(src, src_file), tmp_src_file,
                             quantize)
            src_file = tmp_src_file

        else:
            src_file = os.path.join(src, src_file)

        for file in affected_files[index]:
            if progress.wasCanceled():
                break

            dst_file = os.path.join(dst, file)

            count += 1
            # if count % 25 == 0:
            if time.time() - last_update > MIN_INTERVAL or count % 25 == 0:
                last_update = time.time()
                progress.setLabelText("Importing...\n" + file)
                progress.setValue(count)

                # Re-center the dialog.
                progress_w = progress.geometry().width()
                progress_h = progress.geometry().height()

                new_x = x + ((width - progress_w) / 2)
                new_y = y + ((height - progress_h) / 2)

                progress.move(new_x, new_y)

            # We may be allowed to import files that don't exist, but we're
            # going to ask them about it anyway.
            if not os.path.isfile(dst_file):
                if skip_all_new:
                    continue

                if not import_all_new:
                    answer = QtGui.QMessageBox.question(
                        parent,
                        "File Not Found",
                        "File:\n\n" + file + "\n\n" +
                        "does not exist in the target directory. Import anyway?",
                        buttons=QtGui.QMessageBox.Yes
                        | QtGui.QMessageBox.YesToAll | QtGui.QMessageBox.No
                        | QtGui.QMessageBox.NoToAll,
                        defaultButton=QtGui.QMessageBox.No)

                    if answer == QtGui.QMessageBox.YesToAll:
                        import_all_new = True
                        skip_all_new = False
                    elif answer == QtGui.QMessageBox.NoToAll:
                        skip_all_new = True
                        import_all_new = False
                        continue
                    elif answer == QtGui.QMessageBox.No:
                        continue

            basedir = os.path.dirname(dst_file)
            if not os.path.isdir(basedir):
                os.makedirs(basedir)

            shutil.copy2(src_file, dst_file)

    shutil.rmtree(tmp)
    progress.close()
def export_umdimage(src, dst, convert_gim=True, unique=False, parent=None):
    src = os.path.abspath(src)
    dst = os.path.abspath(dst)
    if os.path.normcase(src) == os.path.normcase(dst):
        raise ValueError(
            "Cannot export %s. Source and destination directories are the same."
            % src)

    answer = QtGui.QMessageBox.question(
        parent,
        "Export Directory",
        "Exporting directory:\n\n" + src + "\n\n" + "into directory:\n\n" +
        dst + "\n\n" + "Proceed?",
        buttons=QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
        defaultButton=QtGui.QMessageBox.No)

    if answer == QtGui.QMessageBox.No:
        return

    progress = QProgressDialog("Exporting...", "Cancel", 0, 0, parent)
    progress.setWindowTitle("Exporting...")
    progress.setWindowModality(Qt.Qt.WindowModal)
    progress.setValue(0)
    progress.setAutoClose(False)
    progress.setMinimumDuration(0)

    if parent:
        width = parent.width()
        height = parent.height()
        x = parent.x()
        y = parent.y()
    else:
        width = 1920
        height = 1080
        x = 0
        y = 0

    seen_groups = []

    count = 0
    last_update = time.time()
    progress.setMaximum(60000)

    for filename in list_all_files(src):
        if progress.wasCanceled():
            break

        count += 1
        if time.time() - last_update > MIN_INTERVAL or count % 25 == 0:
            last_update = time.time()
            progress.setLabelText("Exporting...\n" + filename)
            progress.setValue(count)

            # Re-center the dialog.
            progress_w = progress.geometry().width()
            progress_h = progress.geometry().height()

            new_x = x + ((width - progress_w) / 2)
            new_y = y + ((height - progress_h) / 2)

            progress.move(new_x, new_y)

        base_name = filename[len(src) + 1:]
        if unique:
            dupe_name = os.path.join("umdimage", base_name)
            dupe_name = os.path.normpath(os.path.normcase(dupe_name))

            group = _DUPE_DB.group_from_file(dupe_name)

            if group in seen_groups:
                continue

            if not group == None:
                seen_groups.append(group)

        dst_file = os.path.join(dst, base_name)
        dst_dir = os.path.dirname(dst_file)
        ext = os.path.splitext(dst_file)[1].lower()

        try:
            os.makedirs(dst_dir)
        except:
            pass

        if ext == ".gim" and convert_gim:
            dst_file = os.path.splitext(dst_file)[0] + ".png"
            _CONV.gim_to_png(filename, dst_file)
        else:
            shutil.copy2(filename, dst_file)

    progress.close()
class DatPacker():
    def __init__(self, parent=None):
        self.file_count = 0

        self.parent = parent

    def create_archives(self):

        try:
            self.width = self.parent.width()
            self.height = self.parent.height()
            self.x = self.parent.x()
            self.y = self.parent.y()
        except:
            self.width = 1920
            self.height = 1080
            self.x = 0
            self.y = 0

        self.file_count = 0

        self.progress = QProgressDialog("Reading...", QtCore.QString(), 0,
                                        72000, self.parent)
        self.progress.setWindowModality(Qt.Qt.WindowModal)
        self.progress.setValue(0)
        self.progress.setAutoClose(False)
        self.progress.setMinimumDuration(0)

        # with open(common.editor_config.eboot_orig, "rb") as f:
        with open(
                os.path.join(common.editor_config.iso_dir, "PSP_GAME",
                             "SYSDIR", "EBOOT.BIN"), "rb") as f:
            eboot = BitStream(bytes=f.read())

        eboot, eboot_offset = eboot_patch.apply_eboot_patches(eboot)

        USRDIR = os.path.join(common.editor_config.iso_dir, "PSP_GAME",
                              "USRDIR")

        # So we can loop. :)
        ARCHIVE_INFO = [
            {
                "toc": UMDIMAGES.umdimage,
                "dir": common.editor_config.umdimage_dir,
                "dat": os.path.join(USRDIR, "umdimage.dat"),
                "name": "umdimage.dat",
                "pack": common.editor_config.pack_umdimage,
                "eof": False,
            },
            {
                "toc": UMDIMAGES.umdimage2,
                "dir": common.editor_config.umdimage2_dir,
                "dat": os.path.join(USRDIR, "umdimage2.dat"),
                "name": "umdimage2.dat",
                "pack": common.editor_config.pack_umdimage2,
                "eof": False,
            },
            {
                "toc": None,
                "dir": common.editor_config.voice_dir,
                "dat": os.path.join(USRDIR, "voice.pak"),
                "name": "voice.pak",
                "pack": common.editor_config.pack_voice,
                "eof": True,
            },
            {
                "toc": None,
                "dir": common.editor_config.bgm_dir,
                "dat": os.path.join(USRDIR, "bgm.pak"),
                "name": "bgm.pak",
                "pack": common.editor_config.pack_bgm,
                "eof": True,
            },
        ]

        for archive in ARCHIVE_INFO:

            if not archive["pack"]:
                continue

            self.progress.setWindowTitle("Building " + archive["name"])

            toc_info = {}
            file_list = None

            if archive["toc"]:
                file_list = []

                toc = get_toc(eboot, archive["toc"])

                for entry in toc:
                    filename = entry["filename"]
                    pos_pos = entry["file_pos_pos"]
                    len_pos = entry["file_len_pos"]

                    toc_info[filename] = [pos_pos, len_pos]
                    file_list.append(filename)

            # Causes memory issues if I use the original order, for whatever reason.
            file_list = None

            with io.FileIO(archive["dat"], "w") as handler:
                table_of_contents = self.pack_dir(archive["dir"],
                                                  handler,
                                                  file_list=file_list,
                                                  eof=archive["eof"])

            # We're playing fast and loose with the file count anyway, so why not?
            self.file_count += 1
            self.progress.setValue(self.file_count)
            self.progress.setLabelText("Saving " + archive["name"] + "...")

            if archive["toc"]:
                for entry in table_of_contents:
                    if not entry in toc_info:
                        _LOGGER.warning(
                            "%s missing from %s table of contents." %
                            (entry, archive["name"]))
                        continue

                    file_pos = table_of_contents[entry]["pos"]
                    file_size = table_of_contents[entry]["size"]

                    eboot.overwrite(BitStream(uintle=file_pos, length=32),
                                    toc_info[entry][0] * 8)
                    eboot.overwrite(BitStream(uintle=file_size, length=32),
                                    toc_info[entry][1] * 8)

            del table_of_contents

        self.progress.setLabelText("Saving EBOOT.BIN...")
        self.progress.setValue(self.progress.maximum())

        # Text replacement
        to_replace = eboot_text.get_eboot_text()
        for replacement in to_replace:

            orig = bytearray(replacement.orig, encoding=replacement.enc)

            # If they left something blank, write the original text back.
            if len(replacement.text) == 0:
                data = orig
            else:
                data = bytearray(replacement.text, encoding=replacement.enc)

            pos = replacement.pos.int + eboot_offset

            padding = len(orig) - len(data)
            if padding > 0:
                # Null bytes to fill the rest of the space the original took.
                data.extend(bytearray(padding))

            data = ConstBitStream(bytes=data)
            eboot.overwrite(data, pos * 8)

        eboot_out = os.path.join(common.editor_config.iso_dir, "PSP_GAME",
                                 "SYSDIR", "EBOOT.BIN")

        with open(eboot_out, "wb") as f:
            eboot.tofile(f)

        self.progress.close()

    def pack_dir(self,
                 dir,
                 handler,
                 file_list=None,
                 align_toc=16,
                 align_files=16,
                 eof=False):

        table_of_contents = {}

        if file_list == None:
            file_list = sorted(os.listdir(dir))

        num_files = len(file_list)

        toc_length = (num_files + 1) * 4

        if eof:
            toc_length += 1

        if toc_length % align_toc > 0:
            toc_length += align_toc - (toc_length % align_toc)

        handler.seek(0)
        handler.write(struct.pack("<I", num_files))
        handler.write(bytearray(toc_length - 4))

        for file_num, item in enumerate(file_list):
            full_path = os.path.join(dir, item)

            if os.path.isfile(full_path):

                basename = os.path.basename(item)
                basename, ext = os.path.splitext(basename)

                # Special handling for certain data types.
                if ext == ".txt":
                    data = self.pack_txt(full_path)

                # anagram_81.dat is not a valid anagram file. <_>
                elif basename[:
                              8] == "anagram_" and ext == ".dat" and not basename == "anagram_81":
                    anagram = AnagramFile(full_path)
                    data = anagram.pack(for_game=True).bytes

                else:
                    with open(full_path, "rb") as f:
                        data = f.read()

            else:

                temp_align_toc = 16
                temp_align_files = 4

                if item in SPECIAL_ALIGN:
                    temp_align_toc = SPECIAL_ALIGN[item][0]
                    temp_align_files = SPECIAL_ALIGN[item][1]
                elif os.path.basename(dir) in SPECIAL_ALIGN and len(
                        SPECIAL_ALIGN[os.path.basename(dir)]) == 4:
                    temp_align_toc = SPECIAL_ALIGN[os.path.basename(dir)][2]
                    temp_align_files = SPECIAL_ALIGN[os.path.basename(dir)][3]

                if os.path.splitext(full_path)[1].lower() == ".lin":
                    data = self.pack_lin(full_path)

                else:
                    data = io.BytesIO()
                    with io.BufferedWriter(data) as fh:
                        self.pack_dir(full_path,
                                      fh,
                                      align_toc=temp_align_toc,
                                      align_files=temp_align_files,
                                      eof=eof)
                        fh.flush()
                        data = data.getvalue()

            data = bytearray(data)
            file_size = len(data)
            padding = 0

            if file_size % align_files > 0:
                padding = align_files - (file_size % align_files)
                data.extend(bytearray(padding))

            handler.seek(0, io.SEEK_END)
            file_pos = handler.tell()
            handler.write(data)
            handler.seek((file_num + 1) * 4)
            handler.write(struct.pack("<I", file_pos))

            del data

            self.file_count += 1
            if self.file_count % 25 == 0:
                self.progress.setLabelText("Reading...\n" + full_path)
                self.progress.setValue(self.file_count)

                # Re-center the dialog.
                progress_w = self.progress.geometry().width()
                progress_h = self.progress.geometry().height()

                new_x = self.x + ((self.width - progress_w) / 2)
                new_y = self.y + ((self.height - progress_h) / 2)

                self.progress.move(new_x, new_y)

            table_of_contents[item] = {}
            table_of_contents[item]["size"] = file_size
            table_of_contents[item]["pos"] = file_pos

        if eof:
            handler.seek(0, io.SEEK_END)
            archive_len = handler.tell()
            handler.seek((num_files + 1) * 4)
            handler.write(struct.pack("<I", archive_len))

        return table_of_contents

    def pack_txt(self, filename):

        if os.path.basename(os.path.dirname(filename)) in SCRIPT_NONSTOP:
            is_nonstop = True
        else:
            is_nonstop = False

        text = text_files.load_text(filename)
        text = RE_SCRIPT.sub(u"\g<1>", text)

        # Nonstop Debate lines need an extra newline at the end
        # so they show up in the backlog properly.
        if is_nonstop and not text[-1] == "\n":
            text += "\n"

        return SCRIPT_BOM.bytes + bytearray(
            text, encoding="UTF-16LE") + SCRIPT_NULL.bytes

    def pack_lin(self, dir):

        # Collect our files.
        file_list = sorted(list_all_files(dir))

        txt = [
            filename for filename in file_list
            if os.path.splitext(filename)[1].lower() == ".txt"
        ]
        wrd = [
            filename for filename in file_list
            if os.path.splitext(filename)[1].lower() == ".wrd"
        ]
        py = [
            filename for filename in file_list
            if os.path.splitext(filename)[1].lower() == ".py"
        ]

        # If there are more than one for whatever reason, just take the first.
        # We only have use for a single wrd or python file.
        wrd = wrd[0] if wrd else None
        py = py[0] if py else None

        # Prepare our temporary output directory.
        temp_dir = tempfile.mkdtemp(prefix="sdse-")

        # Where we're outputting our wrd file, regardless of whether it's a python
        # file or a raw binary data file.
        wrd_dst = os.path.join(temp_dir, "0.scp.wrd")

        if py:
            # _LOGGER.info("Compiling %s to binary." % py)
            try:
                wrd_file = WrdFile(py)
            except:
                _LOGGER.warning(
                    "%s failed to compile. Parsing wrd file instead. Exception info:\n%s"
                    % (py, traceback.format_exc()))
                shutil.copy(wrd, wrd_dst)
            else:
                # If we succeeded in loading the python file, compile it to binary.
                # wrd_file.save_bin(wrd)
                wrd_file.save_bin(wrd_dst)

        else:
            shutil.copy(wrd, wrd_dst)

        # Pack the text files in-place to save us a bunch of copying
        # and then move it to the tmp directory with the wrd file.
        if txt:
            with io.FileIO(os.path.join(temp_dir, "1.dat"), "w") as h:
                self.pack_dir(dir, h, file_list=txt)

        # Then pack it like normal.
        data = io.BytesIO()
        with io.BufferedWriter(data) as h:
            self.pack_dir(temp_dir, h)
            h.flush()
            data = data.getvalue()

        shutil.rmtree(temp_dir)

        return data
def import_umdimage(src, dst, convert_png = True, propogate = True, parent = None):
  src = os.path.abspath(src)
  dst = os.path.abspath(dst)
  if os.path.normcase(src) == os.path.normcase(dst):
    raise ValueError("Cannot import %s. Source and destination directories are the same." % src)
    
  answer = QtGui.QMessageBox.question(
    parent,
    "Import Directory",
    "Importing directory:\n\n" + src + "\n\n" +
    "into directory:\n\n" + dst + "\n\n" +
    "Any affected files will be backed up. Proceed?",
    buttons = QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
    defaultButton = QtGui.QMessageBox.No
  )
  
  if answer == QtGui.QMessageBox.No:
    return
  
  progress = QProgressDialog("Finding files...", "Cancel", 0, 1, parent)
  progress.setWindowTitle("Importing...")
  progress.setWindowModality(Qt.Qt.WindowModal)
  progress.setValue(0)
  progress.setAutoClose(False)
  progress.setMinimumDuration(0)
  
  if parent:
    width = parent.width()
    height = parent.height()
    x = parent.x()
    y = parent.y()
  else:
    width   = 1920
    height  = 1080
    x       = 0
    y       = 0
  
  progress.setMaximum(0)
  progress.setValue(0)
  
  # The raw list of files we're importing.
  files = []
  
  # A list of lists, including all dupes of the files being imported, too.
  affected_files = []
  file_count = 0
  
  dupe_base = "umdimage"
  tmp       = tempfile.mkdtemp(prefix = "sdse-")
  
  seen_groups = []
  
  count = 0
  last_update = time.time()
  
  for file in list_all_files(src):
    if progress.wasCanceled():
      break
    
    # Strip our base directory, so we have just a relative file list.
    file = os.path.normpath(os.path.normcase(file[len(src) + 1:]))
    files.append(file)
    
    count += 1
    if time.time() - last_update > MIN_INTERVAL or count % 25 == 0:
      last_update = time.time()
      progress.setLabelText("Finding files...\n" + file)
      # progress.setValue(count)
      progress.setValue(progress.value() ^ 1)
      
      # Re-center the dialog.
      progress_w = progress.geometry().width()
      progress_h = progress.geometry().height()
      
      new_x = x + ((width - progress_w) / 2)
      new_y = y + ((height - progress_h) / 2)
      
      progress.move(new_x, new_y)
    
    affected_files.append([])
    
    if os.path.splitext(file)[1] == ".png" and convert_png and file not in SKIP_CONV:
      file = os.path.splitext(file)[0] + ".gim"
    
    if propogate:
      file_group = _DUPE_DB.group_from_file(os.path.join(dupe_base, file))
    else:
      file_group = None
    
    if file_group in seen_groups:
      continue
    
    # If there are no dupes, just add this file.
    if file_group == None:
      affected_files[-1].append(file)
      file_count += 1
      continue
    
    seen_groups.append(file_group)
    for dupe in _DUPE_DB.files_in_group(file_group):
      # Minus the "umdimage" part
      dupe = dupe[len(dupe_base) + 1:]
      affected_files[-1].append(dupe)
      file_count += 1
  
  progress.setValue(0)
  progress.setMaximum(file_count)
  
  # Make a backup first.
  backup_dir = None
  count = 0
  for file_set in affected_files:
    if progress.wasCanceled():
      break
    for file in file_set:
      if progress.wasCanceled():
        break
      count += 1
      if time.time() - last_update > MIN_INTERVAL or count % 25 == 0:
        last_update = time.time()
        progress.setLabelText("Backing up...\n" + file)
        progress.setValue(count)
        
        # Re-center the dialog.
        progress_w = progress.geometry().width()
        progress_h = progress.geometry().height()
        
        new_x = x + ((width - progress_w) / 2)
        new_y = y + ((height - progress_h) / 2)
        
        progress.move(new_x, new_y)
      
      # It's perfectly possible we want to import some files that
      # don't already exist. Such as when importing a directory
      # with added lines.
      if not os.path.isfile(os.path.join(dst, file)):
        continue
        
      backup_dir = backup_files(dst, [file], suffix = "_IMPORT", backup_dir = backup_dir)
  
  progress.setValue(0)
  
  # And now do our importing.
  import_all_new = False
  skip_all_new = False
  count = 0
  for index, src_file in enumerate(files):
    if progress.wasCanceled():
      break
    
    if os.path.splitext(src_file)[1] == ".png" and convert_png and src_file not in SKIP_CONV:
      tmp_src_file = os.path.join(tmp, os.path.basename(src_file))
      tmp_src_file = os.path.splitext(tmp_src_file)[0] + ".gim"
      quantize = QuantizeType.auto
      for regex, q in FORCE_QUANTIZE:
        if not regex.search(src_file) == None:
          quantize = q
          break
      _CONV.png_to_gim(os.path.join(src, src_file), tmp_src_file, quantize)
      src_file = tmp_src_file
    
    else:
      src_file = os.path.join(src, src_file)
    
    for file in affected_files[index]:
      if progress.wasCanceled():
        break
      
      dst_file = os.path.join(dst, file)
      
      count += 1
      # if count % 25 == 0:
      if time.time() - last_update > MIN_INTERVAL or count % 25 == 0:
        last_update = time.time()
        progress.setLabelText("Importing...\n" + file)
        progress.setValue(count)
        
        # Re-center the dialog.
        progress_w = progress.geometry().width()
        progress_h = progress.geometry().height()
        
        new_x = x + ((width - progress_w) / 2)
        new_y = y + ((height - progress_h) / 2)
        
        progress.move(new_x, new_y)
      
      # We may be allowed to import files that don't exist, but we're
      # going to ask them about it anyway.
      if not os.path.isfile(dst_file):
        if skip_all_new:
          continue
        
        if not import_all_new:
          answer = QtGui.QMessageBox.question(
            parent,
            "File Not Found",
            "File:\n\n" + file + "\n\n" +
            "does not exist in the target directory. Import anyway?",
            buttons = QtGui.QMessageBox.Yes | QtGui.QMessageBox.YesToAll | QtGui.QMessageBox.No | QtGui.QMessageBox.NoToAll,
            defaultButton = QtGui.QMessageBox.No
          )
          
          if answer == QtGui.QMessageBox.YesToAll:
            import_all_new = True
            skip_all_new = False
          elif answer == QtGui.QMessageBox.NoToAll:
            skip_all_new = True
            import_all_new = False
            continue
          elif answer == QtGui.QMessageBox.No:
            continue
      
      basedir = os.path.dirname(dst_file)
      if not os.path.isdir(basedir):
        os.makedirs(basedir)
      
      shutil.copy2(src_file, dst_file)
  
  shutil.rmtree(tmp)
  progress.close()
def export_umdimage(src, dst, convert_gim = True, unique = False, parent = None):
  src = os.path.abspath(src)
  dst = os.path.abspath(dst)
  if os.path.normcase(src) == os.path.normcase(dst):
    raise ValueError("Cannot export %s. Source and destination directories are the same." % src)
    
  answer = QtGui.QMessageBox.question(
    parent,
    "Export Directory",
    "Exporting directory:\n\n" + src + "\n\n" +
    "into directory:\n\n" + dst + "\n\n" +
    "Proceed?",
    buttons = QtGui.QMessageBox.Yes | QtGui.QMessageBox.No,
    defaultButton = QtGui.QMessageBox.No
  )
  
  if answer == QtGui.QMessageBox.No:
    return
  
  progress = QProgressDialog("Exporting...", "Cancel", 0, 0, parent)
  progress.setWindowTitle("Exporting...")
  progress.setWindowModality(Qt.Qt.WindowModal)
  progress.setValue(0)
  progress.setAutoClose(False)
  progress.setMinimumDuration(0)
  
  if parent:
    width = parent.width()
    height = parent.height()
    x = parent.x()
    y = parent.y()
  else:
    width   = 1920
    height  = 1080
    x       = 0
    y       = 0
  
  seen_groups = []
  
  count = 0
  last_update = time.time()
  progress.setMaximum(60000)
  
  for filename in list_all_files(src):
    if progress.wasCanceled():
      break
    
    count += 1
    if time.time() - last_update > MIN_INTERVAL or count % 25 == 0:
      last_update = time.time()
      progress.setLabelText("Exporting...\n" + filename)
      progress.setValue(count)
      
      # Re-center the dialog.
      progress_w = progress.geometry().width()
      progress_h = progress.geometry().height()
      
      new_x = x + ((width - progress_w) / 2)
      new_y = y + ((height - progress_h) / 2)
      
      progress.move(new_x, new_y)
    
    base_name = filename[len(src) + 1:]
    if unique:
      dupe_name = os.path.join("umdimage", base_name)
      dupe_name = os.path.normpath(os.path.normcase(dupe_name))
      
      group = _DUPE_DB.group_from_file(dupe_name)
      
      if group in seen_groups:
        continue
      
      if not group == None:
        seen_groups.append(group)
    
    dst_file = os.path.join(dst, base_name)
    dst_dir  = os.path.dirname(dst_file)
    ext      = os.path.splitext(dst_file)[1].lower()
    
    try:
      os.makedirs(dst_dir)
    except:
      pass
    
    if ext == ".gim" and convert_gim:
      dst_file = os.path.splitext(dst_file)[0] + ".png"
      _CONV.gim_to_png(filename, dst_file)
    else:
      shutil.copy2(filename, dst_file)
  
  progress.close()
 def search_bar(self, query):
   matches = []
   
   progress = QProgressDialog("", "Abort", 0, 50000, self)
   progress.setWindowTitle("Searching...")
   progress.setWindowModality(Qt.Qt.WindowModal)
   progress.setValue(0)
   
   width = self.width()
   height = self.height()
   x = self.x()
   y = self.y()
   
   self.re_flags = re.UNICODE | re.MULTILINE
   
   if not self.ui.chkAdvRegex.isChecked():
     query = re.escape(query)
   
   if not self.ui.chkAdvCase.isChecked():
     self.re_flags |= re.IGNORECASE
   
   if self.ui.chkAdvNewline.isChecked():
     self.re_flags |= re.DOTALL
   
   query_re = re.compile(query, self.re_flags)
   
   dir_filter  = common.qt_to_unicode(self.ui.txtFilterRe.text())
   if dir_filter == "":
     filter_re = script_analytics.DEFAULT_FILTER
   else:
     filter_re = re.compile(dir_filter, re.IGNORECASE | re.DOTALL | re.UNICODE)
   
   self.search_flags = 0
   if self.ui.chkAdvTrans.isChecked():
     self.search_flags |= script_analytics.SEARCH_TRANSLATED
   if self.ui.chkAdvOrig.isChecked():
     self.search_flags |= script_analytics.SEARCH_ORIGINAL
   if self.ui.chkAdvComments.isChecked():
     self.search_flags |= script_analytics.SEARCH_COMMENTS
   
   if self.ui.chkAdvNoTags.isChecked():
     self.search_flags |= script_analytics.SEARCH_NOTAGS
   
   matches = []
   for i, total, filename, partial_results in script_analytics.SA.search_gen(query_re, filter_re, self.search_flags):
     
     if progress.wasCanceled():
       break
     
     progress.setValue(i)
     progress.setMaximum(total)
     progress.setLabelText(filename)
     
     # Re-center the dialog.
     progress_w = progress.geometry().width()
     progress_h = progress.geometry().height()
     
     new_x = x + ((width - progress_w) / 2)
     new_y = y + ((height - progress_h) / 2)
     
     progress.move(new_x, new_y)
     
     matches.extend(partial_results)
   
   progress.close()
   
   return matches
 def search_bar(self, query):
   matches = []
   
   progress = QProgressDialog("", "Abort", 0, 50000, self)
   progress.setWindowTitle("Searching...")
   progress.setWindowModality(Qt.Qt.WindowModal)
   progress.setValue(0)
   progress.setMinimumDuration(0)
   
   width = self.width()
   height = self.height()
   x = self.x()
   y = self.y()
   
   self.re_flags = re.UNICODE | re.MULTILINE
   
   if not self.ui.chkAdvRegex.isChecked():
     query = re.escape(query)
   
   if not self.ui.chkAdvCase.isChecked():
     self.re_flags |= re.IGNORECASE
   
   if self.ui.chkAdvNewline.isChecked():
     self.re_flags |= re.DOTALL
   
   self.query_re = re.compile(query, self.re_flags)
   
   dir_filter  = common.qt_to_unicode(self.ui.txtFilterRe.text())
   if dir_filter == "":
     filter_re = script_analytics.DEFAULT_FILTER
   else:
     filter_re = re.compile(dir_filter, re.IGNORECASE | re.DOTALL | re.UNICODE)
   
   self.search_flags = 0
   if self.ui.chkAdvTrans.isChecked():
     self.search_flags |= script_analytics.SEARCH_TRANSLATED
   if self.ui.chkAdvOrig.isChecked():
     self.search_flags |= script_analytics.SEARCH_ORIGINAL
   if self.ui.chkAdvComments.isChecked():
     self.search_flags |= script_analytics.SEARCH_COMMENTS
   
   if self.ui.chkAdvNoTags.isChecked():
     self.search_flags |= script_analytics.SEARCH_NOTAGS
   
   matches = []
   for i, total, filename, partial_results in script_analytics.SA.search_gen(self.query_re, filter_re, self.search_flags):
     
     if progress.wasCanceled():
       break
     
     progress.setValue(i)
     progress.setMaximum(total)
     progress.setLabelText(filename)
     
     # Re-center the dialog.
     progress_w = progress.geometry().width()
     progress_h = progress.geometry().height()
     
     new_x = x + ((width - progress_w) / 2)
     new_y = y + ((height - progress_h) / 2)
     
     progress.move(new_x, new_y)
     
     matches.extend(partial_results)
   
   progress.close()
   
   return matches