# coding:utf-8
"""
    MoinMoin ubuntu theme

    @copyright: (c) 2003-2004 by Nir Soffer, Thomas Waldmann
    @license: GNU GPL, see COPYING for details.

    Borrows heavily from modern, a bit from Classic, and has a few
    things of its own.
    Zoomq::060426 fixed from balanced2
"""

from MoinMoin.theme import ThemeBase
from MoinMoin import wikiutil
from MoinMoin.Page import Page
import re

class Theme(ThemeBase):
    name = 'woodpecker'
	
    # Overriding theme/__init__.py #########################################

    # fake _ function to get gettext recognize those texts:
    _ = lambda x: x

    # TODO: remove icons that are not used any more.
    icons = {
        # key         alt                        icon filename      w   h
        # ------------------------------------------------------------------
        # navibar
        'help':       ("%(page_help_contents)s", "s-help.png",         20, 20),
        'find':       ("%(page_find_page)s",     "s-search.png",       20, 20),
        'diff':       (_("Diffs"),               "diff.png",         22, 22),
        'info':       (_("Info"),                "info.png",         22, 22),
        'edit':       (_("Edit"),                "edit.png",         20, 20),
        'unsubscribe':(_("Unsubscribe"),         "unsubscribe.png",  23, 21),
        'subscribe':  (_("Subscribe"),           "subscribe_dk.png", 23, 21),
        'raw':        (_("Raw"),                 "raw.png",          21, 23),
        'xml':        (_("XML"),                 "moin-xml2.png",      36, 14),
        'print':      (_("Print"),               "print.png",        20, 20),
        'view':       (_("View"),                "show.png",         20, 20),
        'home':       (_("Home"),                "home.png",         20, 20),
        'up':         (_("Up"),                  "parent.png",       20, 20),
        # FileAttach
        'attach':     ("%(attach_count)s",       "moin-attach.png",  7, 15),
        # RecentChanges
        'rss':        (_("[RSS]"),               "moin-rss.png",    36, 14),
        'deleted':    (_("[DELETED]"),           "deleted.png",      22, 22),
        'updated':    (_("[UPDATED]"),           "updated.png",      22, 22),
        'new':        (_("[NEW]"),               "new.png",          22, 22),
        'diffrc':     (_("[DIFF]"),              "diff.png",         22, 22),
        # General
        'bottom':     (_("[BOTTOM]"),            "u-bottom.png",       14, 10),
        'top':        (_("[TOP]"),               "u-top.png",          14, 10),
        'www':        ("[WWW]",                  "www.png",          16, 16),
        'mailto':     ("[MAILTO]",               "email.png",        23, 13),
        'news':       ("[NEWS]",                 "news.png",         16, 16),
        'telnet':     ("[TELNET]",               "telnet.png",       16, 16),
        'ftp':        ("[FTP]",                  "ftp.png",          16, 16),
        'file':       ("[FILE]",                 "ftp.png",          16, 16),
        # search forms
        'searchbutton': ("[?]",                  "s-search.png",       20, 20),
        'interwiki':  ("[%(wikitag)s]",          "inter.png",        16, 16),
    }
    del _

    # Public functions #####################################################

    def header(self, d, **kw):
        """ Assemble wiki header

        @param d: parameter dictionary
        @rtype: unicode
        @return: page header html
        """
        html = [
            # Header
            u'<div id="mastwrap">',
            u'<div id="masthead">',
            self.logo(),
            self.searchform(d),
            u'<div id="locationline">',
            self.interwiki(d),
            self.title(d),
            u'</div> <!-- 4 locationline -->',

            u'<div id="pagetrail">',
            self.trail(d),
            u'</div> <!-- 4 pagetrail -->',

            ##Zoomq::060418 usage
            #self.sisternav(),
            self.navibar(d),
            # Zoomq::060418 fixed for woodpecker

            u'</div> <!-- of masthead -->',
            u'</div> <!-- of mastwrap -->',
            ## Zoomq::060418 not Columns
            u'<div id="editbar">',
            self.username(d),
            self.msg(d),
            self.editbar(d),
            u'</div>',

            u'<div id="outerColumnContainer"><div id="innerColumnContainer">',

            #u'<div id="leftColumn"><div class="inside">Left',
            #self.leftsidemenu(d),
            #u'</div></div> <!-- of leftColumn & inside -->',

            #u'<div id="rightColumn"><div class="inside">Right',
            #self.rightsidemenu(d),
            #u'</div></div> <!-- of rightColumn & inside -->',

            u'<div id="contentColumn"><div class="inside">',
            #u'<div id="pageline"><hr style="display:none;"></div>',
            ]
        return u'\n'.join(html)


    def footer(self, d, **keywords):
        """ Assemble wiki footer

        @param d: parameter dictionary
        @keyword ...:...
        @rtype: unicode
        @return: page footer html
        """
        page = d['page']
	## zoomq::060812 for Yahoo! search support
	ysearcode = """
<!-- START CODE 
			background:#FFFFFF; 
--> 

	<div id="ysrchForm" style=" 
			border:1px solid #7E9DB9;

			width:350px;			 
			margin:0 auto; 
			padding:0px; position:relative;">

          <form id="searchBoxForm_U0RNxnDNGDJdIAAEc2eOU" action="http://builder.search.yahoo.com/a/bouncer" style="padding:0;">
                        <input name="mobid" value="U0RNxnDNGDJdIAAEc2eOU" type="hidden">
			<input name="ei" value="UTF-8" type="hidden">
			<input name="fr" value="ystg-c" type="hidden"><div style="padding:0 30px 0 0;zoom:1;">
			Searching all CPUG sites and mailist::<br/>
					<input type="text" id="searchTerm"
						onFocus="this.style.background='#fff';"
						onBlur="if(this.value=='')this.style.background='#fff url(http://us.i1.yimg.com/us.yimg.com/i/us/sch/gr/horiz_pwrlogo_red2.gif) 3px center  no-repeat'"
						name="p" style=" margin:1px 0; width:100%; border:1px solid #7E9DB9; color:#666666; height:18px; padding:1px 3px; background:#fff url(http://us.i1.yimg.com/us.yimg.com/i/us/sch/gr/horiz_pwrlogo_red2.gif) 3px center no-repeat; position:relative;">
					<input type="submit" id="btn_U0RNxnDNGDJdIAAEc2eOU" value="Search" 
						style=" padding-bottom:2px; position:absolute; right:10px; top:20px; margin:0px; height:22px; width:65px; ">
</div>

<ul style="color:#666666;
           font:11px/11px normal Arial, Helvetica, sans-serif;
	   margin:0;
	   padding:0;
	   text-align:left;
	   list-style-type:none;radios">
<li style="display:inline;padding-right:10px;">
<input name="mobvs" id="web_U0RNxnDNGDJdIAAEc2eOU" value="0" onclick='displayPopSearch("web","U0RNxnDNGDJdIAAEc2eOU");'   type="hidden">
<label for="web_U0RNxnDNGDJdIAAEc2eOU" style="vertical-align:middle;display:none;">Web</label>

</li></ul></form></div>
<!-- END CODE --> 
	"""

        html = [
            u'</div></div> <!-- of contentColumn & inside -->',
            u'<div class="clear mozclear"></div>',

            u'</div></div> <!-- of outerColumnContainer & innerColumnContainer -->',
            # Footer
            u'<div id="footer" class="inside">',
            #self.editbar(d),
            self.credits(d),
	    # zoomq::060811 support yahoo! searching
	    ysearcode,
            self.showversion(d, **keywords),
            #self.footerlinks(),
            ### Zoomq::060626 add for try pipi
            #u'<script language="javascript" src="http://hi.51pipi.com/container_1_2.js" ></script>',
            u'</div>',
            ]
        return u'\n'.join(html)






    def extranav(self, d):
        """ Assemble the helpful extra navigation

        Of course in a normal theme these come from wikiconfig.py

        @param d: parameter dictionary
        @rtype: unicode
        @return: extranav html
        """
        request = self.request
        _ = request.getText
        changesPage = wikiutil.getSysPage(request, 'RecentChanges')
        findPage = wikiutil.getSysPage(request, 'FindPage')

        extralinks = []
        # Set page to localized RC page
        title = changesPage.split_title(request)
        extralinks.append(changesPage.link_to(request, text=title))
        # Set page to localized find page
        title = findPage.split_title(request)
        extralinks.append(findPage.link_to(request, text=title))

        extralinks = [u'<li>%s</li>\n' % link for link in extralinks]
        html = u'<ul class="extranav">\n%s</ul>' % ''.join(extralinks)

        return html




    def leftsidemenu(self, d):
        """ Assemble the side menu """

        html = 'error'
        menuTag = "##MENU .*html"
        menuPath = "C:\\APPS\\MMDE\\wiki\\data\\pages\\MenuPages\\attachments\\"  # FIXME: abstract out the path
        pageBody = self.request.page.get_raw_body()
        menuLineObj = re.search(menuTag, pageBody)
        # catch bad or missing tag
        if menuLineObj:
            menuLine = menuLineObj.group()
            fileName = re.sub("##MENU ","",menuLine)
        else:
            fileName = 'sidemenu-default.html'

        # catch non-existant files
        try:
            fullName = menuPath+fileName
            fileHandle = open(fullName)
        except IOError:
            fileName = 'sidemenu-default.html'
            fullName = menuPath+fileName
            fileHandle = open(fullName)

        html = fileHandle.read()
        fileHandle.close()
        return html


    def rightsidemenu(self, d):
        """ Assemble the side menu """

        html = 'error'
        menuTag = "##MENU .*html"
        menuPath = "C:\\APPS\\MMDE\\wiki\\data\\pages\\MenuPages\\attachments\\"  # FIXME: abstract out the path
        pageBody = self.request.page.get_raw_body()
        menuLineObj = re.search(menuTag, pageBody)
        # catch bad or missing tag
        if menuLineObj:
            menuLine = menuLineObj.group()
            fileName = re.sub("##MENU ","",menuLine)
        else:
            fileName = 'sidemenu-default.html'

        # catch non-existant files
        try:
            fullName = menuPath+fileName
            fileHandle = open(fullName)
        except IOError:
            fileName = 'sidemenu-default.html'
            fullName = menuPath+fileName
            fileHandle = open(fullName)

        html = fileHandle.read()
        fileHandle.close()
        return html



    def username(self, d):
        """ Assemble the username / userprefs link

        @param d: parameter dictionary
        @rtype: unicode
        @return: username html
        @note:  lost logout link usage normal username
        """
        request = self.request
        _ = request.getText
        preferencesPage = wikiutil.getSysPage(request, 'UserPreferences')
        helpPage = wikiutil.getSysPage(request, 'HelpContents')

        userlinks = []
        # Add username/homepage link for registered users. We don't care
        # if it exists, the user can create it.
        if request.user.valid:
            homepage = Page(request, request.user.name)
            title = homepage.split_title(request)
            homelink = homepage.link_to(request, text=title)
            userlinks.append(homelink)
            # Set pref page to localized Preferences page
            title = preferencesPage.split_title(request)
            userlinks.append(preferencesPage.link_to(request, text=title))
            #userlinks.append(helpPage.link_to(request, text=_("Help")))
        else:
            # Add prefpage links with title: Login
            userlinks.append(preferencesPage.link_to(request, text=_("Login")))

        if request.cfg.show_login:
            if request.user.valid:
                userlinks.append(d['page'].link_to(request, text=_('Logout', formatted=False),
                                                   querystr={'action': 'logout', 'logout': 'logout'}, id="logout"))
            else:
                userlinks.append(d['page'].link_to(request, text=_("Login", formatted=False),
                                                   querystr={'action': 'login'}, id="login"))

        userlinks = [u'<li>%s</li>\n' % link for link in userlinks]
        html = u'<ul id="username">\n%s</ul>' % ''.join(userlinks)
        return html


    def sisternav(self):
        """ Assemble fancy nav tabs to the sister sites
        """

        html = u'''<div id="sisternav">
      <ul>
        <li id="current"><a href="/moin/FrontPage" 首页</a></li>
        <li id="plain"><a href="/moin/RecentChanges" >更新</a></li>
        <li id="plain"><a href="/moin/FindPage" >搜索</a></li>
        <li id="plain"><a href="/moin/HelpContents" >帮助</a></li>
      </ul>
    </div>'''
        return html

    def footerlinks(self):
        """ Copyright notices and local links """
        html = u'''
     &copy; Henrik Nilsen Omma
     <a href="/UserPreferences">Admin</a>
            '''
        return html





def execute(request):
    """
    Generate and return a theme object

    @param request: the request object
    @rtype: MoinTheme
    @return: Theme object
    """

    return Theme(request)
