import sys
import os
import datetime
from email.utils import parsedate
import time
from workflow import Workflow, PasswordNotFound
from dropbox import client, rest
import config


def main(wf):
    user_input = ''.join(wf.args)

    try:
        wf.get_password('dropbox_access_token')
        # user_input = '/.ws.agile.1Password.settings'
        file_or_folder = get_file_or_folder(user_input)

        if len(file_or_folder) > 0:
            for f in file_or_folder:
                if user_input == f['path']:
                    wf.add_item(
                        'Share', 'Copy link to clipboard', arg='share ' + f['path'], icon='dbicons/folder_public.png', valid=True)
                    wf.add_item(
                        'Save to Downloads', arg='download ' + f['path'], icon='icons/download.png', valid=True)
                    wf.add_item(
                        'Save to Desktop', arg='desktop ' + f['path'], icon='icons/desktop.png', valid=True)
                    wf.add_item(
                        'Delete', arg='delete ' + f['path'], valid=True)
                else:
                    title = os.path.basename(f['path'])
                    subtitle = 'Modified: ' + \
                        time.strftime(
                            '%Y-%m-%d %H:%M:%S', parsedate(f['modified']))
                    icon = 'dbicons/' + f['icon'] + '.png'
                    if f['is_dir']:
                        title += '/'
                        wf.add_item(
                            title, subtitle, icon=icon, autocomplete=f['path'], valid=False)
                    else:
                        title += ' (' + f['size'] + ')'
                        wf.add_item(
                            title, subtitle, icon=icon, autocomplete=f['path'], valid=False)
        else:
            wf.add_item(
                'Folder seems to be empty.', 'Try another one!', valid=False)

    except PasswordNotFound:
        wf.add_item(
            'Please press enter and click on "Allow" on Dropbox\'s website', 'Then use the code to authorize the workflow.', arg='url ' + get_auth_url(), valid=True)
        wf.add_item(
            'Authorize with "%s"' % user_input, 'Press enter to proceed.', arg='auth ' + user_input, valid=True)

    wf.send_feedback()


def get_file_or_folder(dir='/'):
    access_token = wf.get_password('dropbox_access_token')
    api_client = client.DropboxClient(access_token)
    try:
        resp = api_client.metadata(dir)

        output = []
        if 'contents' in resp:
            output = resp['contents']
        else:
            output.append(resp)
        wf.cache_data('last_output', output)
        return output
    except rest.ErrorResponse, e:
        return wf.cached_data('last_output')


def get_auth_url():
    flow = client.DropboxOAuth2FlowNoRedirect(
        config.APP_KEY, config.APP_SECRET)
    return flow.start()


if __name__ == '__main__':
    wf = Workflow()
    sys.exit(wf.run(main))

@@ -123,16 +123,28 @@
 NotFound
+, ICON_TRASH
 %0Afrom dr
@@ -295,63 +295,8 @@
 n')%0A
-        # user_input = '/.ws.agile.1Password.settings'%0A
     
@@ -1006,24 +1006,41 @@
 + f%5B'path'%5D,
+ icon=ICON_TRASH,
  valid=True)
@@ -1519,32 +1519,38 @@
 mplete=f%5B'path'%5D
+ + '/'
 , valid=False)%0A 
@@ -1820,52 +1820,55 @@
    '
-Folder seems to be empty.', 'Try another one
+No files were found.', 'Try a different request
 !', 
@@ -2284,18 +2284,86 @@
 der(
-dir
+path
 ='/'):%0A
+    if len(path) %3E 1 and path%5B-1%5D == '/':%0A        path = path%5B:-1%5D%0A
     
@@ -2469,16 +2469,32 @@
 _token)%0A
+    output = %5B%5D%0A
     try:
@@ -2533,33 +2533,29 @@
 ata(
-dir)%0A%0A        output = %5B%5D
+path, file_limit=100)
 %0A   
@@ -2666,16 +2666,57 @@
 d(resp)%0A
+        wf.cache_data('last_path', path)%0A
         
@@ -2816,22 +2816,27 @@
         
-return
+last_path =
  wf.cach
@@ -2853,16 +2853,244 @@
 ast_
+path')%0A        if last_path in path:%0A            query = path%5Blen(last_path) + 1:%5D%0A            log.debug(last_path)%0A            log.debug(query)%0A            output = api_client.search(last_path, query, file_limit=100)%0A%0A    return 
 output
-')
 %0A%0A%0Ad
@@ -3271,16 +3271,36 @@
 kflow()%0A
+    log = wf.logger%0A
     sys.

Add search support, change delete icon