#!/usr/bin/env python
#
# 2005-02-12    initial version    hp

from __future__ import print_function
import os
import pickle
from PyTQt.tqt import *
from dirview import Directory, DirectoryView

bookmarks = [
    "22 14 8 1",
    "# c #000080",
    "a c #585858",
    "b c #000000",
    "c c #ffffff",
    "d c #ffffff",
    "e c #ffffff",
    "f c #000000",
    ". c None",
    "...bb.................",
    "..bacb....bbb.........",
    "..badcb.bbccbab.......",
    "..bacccbadccbab.......",
    "..baecdbcccdbab.......",
    "..bacccbacccbab.......",
    "..badcdbcecdfab.......",
    "..bacecbacccbab.......",
    "..baccdbcccdbab.......",
    "...badcbacdbbab.......",
    "....bacbcbbccab.......",
    ".....babbaaaaab.......",
    ".....bbabbbbbbb.......",
    "......bb.............."
]

home = [
    "16 15 4 1",
    "# c #000000",
    "a c #ffffff",
    "b c #c0c0c0",
    ". c None",
    ".......##.......",
    "..#...####......",
    "..#..#aabb#.....",
    "..#.#aaaabb#....",
    "..##aaaaaabb#...",
    "..#aaaaaaaabb#..",
    ".#aaaaaaaaabbb#.",
    "###aaaaaaaabb###",
    "..#aaaaaaaabb#..",
    "..#aaa###aabb#..",
    "..#aaa#.#aabb#..",
    "..#aaa#.#aabb#..",
    "..#aaa#.#aabb#..",
    "..#aaa#.#aabb#..",
    "..#####.######.."
]


class PixmapView(TQScrollView):
    def __init__(self, parent):
        TQScrollView.__init__(self, parent)
        self.pixmap = None
        self.viewport().setBackgroundMode(self.PaletteBase)

    def setPixmap(self, pix):
        self.pixmap = pix
        self.resizeContents(pix.size().width(), pix.size().height())
        self.viewport().repaint(False)

    def drawContents(self, p, cx, cy, cw, ch):
        p.fillRect(cx, cy, cw, ch, self.colorGroup().brush(TQColorGroup.Base))
        p.drawPixmap(0, 0, self.pixmap)


class Preview(TQWidgetStack):
    def __init__(self, parent):
        TQWidgetStack.__init__(self, parent)
        self.normalText = TQMultiLineEdit(self)
        self.normalText.setReadOnly(True)
        self.html = TQTextView(self)
        self.pixmap = PixmapView(self)
        self.raiseWidget(self.normalText)

    def showPreview(self, url, size):
        if url.isLocalFile():
            path = url.path()
            fi = TQFileInfo(path)
            if fi.isFile() and fi.size() > size * 1000:
                self.normalText.setText(
                    "The File\n%s\nis too large, so I don't show it!" % path)
                self.raiseWidget(self.normalText)
                return
            pix = TQPixmap(path)
            if pix.isNull():
                if fi.isFile():
                    err = False
                    try:
                        text = open(path.latin1(), "r").read()
                    except IOError as msg:
                        text = TQString(str(msg))
                        err = True
                    if not err and fi.extension().lower().contains("htm"):
                        url = self.html.mimeSourceFactory().makeAbsolute(
                                                path, self.html.context())
                        self.html.setText(text, url)
                        self.raiseWidget(self.html)
                        return
                    else:
                        self.normalText.setText(text)
                        self.raiseWidget(self.normalText)
                        return
                else:
                    self.normalText.setText("")
                    self.raiseWidget(self.normalText)
            else:
                self.pixmap.setPixmap(pix)
                self.raiseWidget(self.pixmap)
        else:
            self.normalText.setText("I only show local files!")
            self.raiseWidget(self.normalText)


# We can't instantiate TQFilePreview directly because it is abstract.  Note that
# the previewUrl() abstract is patched in later to work around the fact that
# you can't multiply inherit from more than one wrapped class.
class FilePreview(TQFilePreview):
    pass


class PreviewWidget(TQVBox):
    def __init__(self, parent):
        TQVBox.__init__(self, parent)
        self.setSpacing( 5 )
        self.setMargin( 5 )
        row = TQHBox(self)
        row.setSpacing(5)
        TQLabel("Only show files smaller than: ", row)
        self.sizeSpinBox = TQSpinBox(1, 10000, 1, row)
        self.sizeSpinBox.setSuffix(" KB")
        self.sizeSpinBox.setValue(128)
        row.setFixedHeight(10 + self.sizeSpinBox.sizeHint().height())
        self.__preview = Preview(self)
        # workaround sip inability of multiple inheritance
        # create a local TQFilePreview instance and redirect
        # the method, which is called on preview, to us
        self.preview = FilePreview()
        self.preview.previewUrl = self.previewUrl

    def previewUrl(self, url):
        self.__preview.showPreview(url, self.sizeSpinBox.value())


class CustomFileDialog(TQFileDialog):
    def __init__(self, preview = False):
        TQFileDialog.__init__(self, None, None, True)
        self.bookmarkFile = ".pybookmarks"
        self.bookmarkList = []
        if os.path.exists(self.bookmarkFile):
            try:
                self.bookmarkList = pickle.loads(open(self.bookmarkFile, "rb").read())
            except IOError as msg:
                print(msg)
        self.setDir("/")
        self.dirView = DirectoryView(self, None, True)
        self.dirView.addColumn("")
        self.dirView.header().hide()
        root = Directory(self.dirView, "/")
        root.setOpen(True)
        self.dirView.setFixedWidth(200)
        self.addLeftWidget(self.dirView)
        p = TQPushButton(self)
        p.setPixmap(TQPixmap(bookmarks))
        TQToolTip.add(p, "Bookmarks")
        self.bookmarkMenu = TQPopupMenu(self)
        self.connect(self.bookmarkMenu, TQ_SIGNAL("activated(int)"),
             self.bookmarkChosen)
        self.addId = self.bookmarkMenu.insertItem("Add bookmark")
        self.bookmarkMenu.insertSeparator()
        for l in self.bookmarkList:
            self.bookmarkMenu.insertItem(l)
        p.setPopup(self.bookmarkMenu)
        self.addToolButton(p, True)
        self.connect(self.dirView, PYSIGNAL("folderSelected(const TQString &)"),
             self.setDir2)
        self.connect(self, TQ_SIGNAL("dirEntered(const TQString &)"),
             self.dirView.setDir)
        b = TQToolButton(self)
        TQToolTip.add(b, "Go Home!")
        b.setPixmap(TQPixmap(home))
        self.connect(b, TQ_SIGNAL("clicked()"), self.goHome)
        self.addToolButton(b)

        if preview:
            self.setContentsPreviewEnabled(True)
            pw = PreviewWidget(self)
            self.setContentsPreview(pw, pw.preview)
            self.setViewMode(TQFileDialog.List)
            self.setPreviewMode(TQFileDialog.Contents)

        w = self.width()
        h = self.height()
        if preview:
            self.resize(w + w / 2, h + h / 3)
        else:
            self.resize(w + w / 3, h + h / 4)

    def done(self, r):
        if self.bookmarkList:
            try:
                open(self.bookmarkFile, "wb").write(pickle.dumps(self.bookmarkList))
            except IOError as msg:
                print(msg)
        return TQFileDialog.done(self, r)

    def showEvent(self, e):
        TQFileDialog.showEvent(self, e)
        self.dirView.setDir(self.dirPath())

    def setDir2(self, path):
        self.blockSignals(True)
        self.setDir(path)
        self.blockSignals(False)

    def bookmarkChosen(self, i):
        if i == self.addId:
            # keep bookmarks pythonic
            dp = self.dirPath().latin1()
            if dp not in self.bookmarkList:
                self.bookmarkList.append(dp)
                self.bookmarkMenu.insertItem(dp)
        else:
            self.setDir(self.bookmarkMenu.text(i))

    def goHome(self):
        if os.getenv("HOME"):
            self.setDir(os.getenv("HOME"))
        else:
            self.setDir("/")


if __name__ == '__main__':
    import sys
    import getopt

    def usage(msg = None):
        if msg:
            print(msg, file=sys.stderr)
        print("""\
usage: tqdir [--any | --dir | --custom] [--preview] [--default f] {--filter f} [caption ...]
     --any         Get any filename, need not exist.
     --dir         Return a directory rather than a file.
     --custom      Opens a customized TQFileDialog with
                   dir browser, bookmark menu, etc.
     --preview     Show a preview widget.
     --default f   Start from directory/file f.
     --filter f    eg. '*.gif' '*.bmp'
     caption ...   Caption for dialog.
""", file=sys.stderr)
        sys.exit(1)

    def main():
        options = ["help", "any", "dir", "custom", "preview", "default=", "filter="]
        mode = TQFileDialog.ExistingFile
        preview = False
        custom = False
        start = None
        filter = TQString.null
        app = TQApplication(sys.argv)
    
        try:
            optlist, args = getopt.getopt(sys.argv[1:], "h", options)
        except getopt.error as msg:
            usage(msg)
    
        for opt, par in optlist:
            if opt in ("-h", "--help"):
                usage()
            elif opt == "--any":
                mode = TQFileDialog.AnyFile
            elif opt == "--dir":
                mode = TQFileDialog.Directory
            elif opt == "--default":
                start = par
            elif opt == "--filter":
                filter = par
            elif opt == "--preview":
                preview = True
            elif opt == "--custom":
                custom = True
        if args:
            caption = " ".join(args)
        elif mode == TQFileDialog.Directory:
            caption = "Choose directory..."
        else:
            caption = "Choose file..."
        if not start:
            start = TQDir.currentDirPath()
        if not custom:
            fd = TQFileDialog(TQString.null, filter, None, None, True)
            fd.setMode(mode)
            if preview:
                fd.setContentsPreviewEnabled(True)
                pw = PreviewWidget(fd)
                fd.setContentsPreview(pw, pw.preview)
                fd.setViewMode(TQFileDialog.List)
                fd.setPreviewMode(TQFileDialog.Contents)
                w = fd.width()
                h = fd.height()
                fd.resize(w + w / 3, h + h / 4)
            fd.setCaption(caption)
            fd.setSelection(start)
            if fd.exec_loop() == TQDialog.Accepted:
                print("%s\n" % fd.selectedFile().latin1())
                return 0
            else:
                return 1
        else:
            fd = CustomFileDialog(preview)
            fd.exec_loop()
        return 1

    sys.exit(main())
