import cherrypy
from gettext import gettext as _
from modules.auth import require
from plugin_mount import PagePlugin, FormPlugin
from forms import Form
import actions
import cfg
from util import Message

class Owncloud(PagePlugin, FormPlugin):
    order = 90

    def __init__(self, *args, **kwargs):
        PagePlugin.__init__(self, *args, **kwargs)
        self.register_page("apps.owncloud")
        cfg.html_root.apps.menu.add_item("Owncloud", "icon-picture", "/apps/owncloud", 35)

    @cherrypy.expose
    @require()
    def index(self, **kwargs):
        output, error = actions.run("owncloud-setup", 'status')
        if error:
            raise Exception("something is wrong: " + error)
        owncloud_enable = "enable" in output.split()

        if 'submitted' in kwargs:
            owncloud_enable = self.process_form(kwargs)
        
        main = self.form(owncloud_enable)
        sidebar_right="""
<strong>ownCloud</strong></br>
<p>ownCloud gives you universal access to your files through a web interface or WebDAV. It also provides a platform to easily view & sync your contacts, calendars and bookmarks across all your devices and enables basic editing right on the web. Installation has minimal server requirements, doesn't need special permissions and is quick. ownCloud is extendable via a simple but powerful API for applications and plugins.
</p>
"""
        return self.fill_template(title="Owncloud", main=main, sidebar_right=sidebar_right)

    def form(self, owncloud_enable, message=None):
        form = Form(title="Configuration",
                        action=cfg.server_dir + "/apps/owncloud/index",
                        name="configure_owncloud",
                        message=message)
        form.checkbox(_("Enable Owncloud"), name="owncloud_enable", id="owncloud_enable", checked=owncloud_enable)
        # hidden field is needed because checkbox doesn't post if not checked
        form.hidden(name="submitted", value="True")
        form.html(_("""<p>When enabled, the owncloud installation will be available from <a href="/owncloud">owncloud</a> on the web server.</p>"""))
        form.submit(_("Update setup"))
        return form.render()

    def process_form(self, kwargs):
        checkedinfo = {
            'enable'   : False,
        }

        opts = []
        for k in kwargs.keys():
            if 'on' == kwargs[k]:
                shortk = k.split("owncloud_").pop()
                checkedinfo[shortk] = True

            for key in checkedinfo.keys():
                if checkedinfo[key]:
                    opts.append(key)
                else:
                    opts.append('no'+key)
        actions.superuser_run("owncloud-setup", opts, async=True)

        return checkedinfo['enable']

@@ -172,16 +172,31 @@
 ort cfg%0A
+import service%0A
 from uti
@@ -505,70 +505,267 @@
     
-@cherrypy.expose%0A    @require()%0A    def index(self, **kwargs):
+    self.service = service.Service('owncloud', _('ownCloud'),%0A                                       %5B'http', 'https'%5D,%0A                                       enabled=self.is_enabled)%0A%0A    def is_enabled(self):%0A        %22%22%22Return whether ownCloud is enabled%22%22%22
 %0A   
@@ -793,25 +793,25 @@
 actions.run(
-%22
+'
 owncloud-set
@@ -812,17 +812,17 @@
 ud-setup
-%22
+'
 , 'statu
@@ -875,90 +875,205 @@
 ion(
-%22someth
+'Error gett
 ing 
-is wrong: %22 + error)%0A        owncloud_enable = %22enable%22 in output.split
+ownCloud status: %25s' %25 error)%0A%0A        return 'enable' in output.split()%0A%0A    @cherrypy.expose%0A    @require()%0A    def index(self, **kwargs):%0A        owncloud_enable = self.is_enabled
 ()%0A%0A
@@ -3046,16 +3046,170 @@
 =True)%0A%0A
+        # Send a signal to other modules that the service is%0A        # enabled/disabled%0A        self.service.notify_enabled(self, checkedinfo%5B'enable'%5D)%0A%0A
         

Make ownCloud work with service framework