def load(self, filename):
        if not os.path.isfile(filename):
            QtGui.QMessageBox.critical(self, "File Not Found",
                                       "Couldn't find \"" + filename + "\"")
            self.reject()
            return

        self.anagram = AnagramFile(filename)

        self.setWindowTitle("Anagram Editor - " + os.path.basename(filename))

        self.ui.txtSolutionTrans.setText(self.anagram.solution.translated)
        self.ui.txtSolutionOrig.setText(self.anagram.solution.original)
        self.ui.txtExtraTrans.setText(self.anagram.extra.translated)
        self.ui.txtExtraOrig.setText(self.anagram.extra.original)

        self.set_checks(CHK_TYPE.Trans, CHK_DIFF.Easy, self.anagram.easy)
        self.set_checks(CHK_TYPE.Trans, CHK_DIFF.Norm, self.anagram.normal)
        self.set_checks(CHK_TYPE.Trans, CHK_DIFF.Hard, self.anagram.hard)

        self.set_checks(CHK_TYPE.Orig, CHK_DIFF.Easy, self.anagram.easy_orig)
        self.set_checks(CHK_TYPE.Orig, CHK_DIFF.Norm, self.anagram.normal_orig)
        self.set_checks(CHK_TYPE.Orig, CHK_DIFF.Hard, self.anagram.hard_orig)

        # The translation side is updated automatically because the text box changes.
        self.update_checks(CHK_TYPE.Orig)
        self.update_preview()
    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