2018-08-26 08:13:54 +01:00
|
|
|
# Copyright 2016 Mycroft AI Inc.
|
2017-04-04 19:04:14 +01:00
|
|
|
#
|
2018-08-26 08:13:54 +01:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
2017-04-04 19:04:14 +01:00
|
|
|
#
|
2018-08-26 08:13:54 +01:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2017-04-04 19:04:14 +01:00
|
|
|
#
|
2018-08-26 08:13:54 +01:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2017-04-04 19:04:14 +01:00
|
|
|
import webbrowser
|
2017-06-25 19:42:30 +01:00
|
|
|
import subprocess
|
2017-04-04 19:04:14 +01:00
|
|
|
|
|
|
|
from adapt.intent import IntentBuilder
|
|
|
|
from adapt.tools.text.tokenizer import EnglishTokenizer
|
|
|
|
|
2019-11-06 10:34:08 +00:00
|
|
|
from mycroft import MycroftSkill, intent_handler
|
2019-03-03 17:01:46 +00:00
|
|
|
from urllib.parse import quote
|
|
|
|
from gi.repository import Gio as gio
|
2017-04-04 19:04:14 +01:00
|
|
|
|
2019-11-06 10:34:08 +00:00
|
|
|
IFL_TEMPLATE = 'http://www.google.com/search?&sourceid=navclient&btnI=I&q=%s'
|
2017-04-04 19:04:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DesktopLauncherSkill(MycroftSkill):
|
|
|
|
def __init__(self):
|
2019-11-06 10:34:08 +00:00
|
|
|
super().__init__()
|
2017-04-04 19:04:14 +01:00
|
|
|
self.appmap = {}
|
|
|
|
|
|
|
|
def initialize(self):
|
|
|
|
tokenizer = EnglishTokenizer()
|
|
|
|
|
|
|
|
for app in gio.app_info_get_all():
|
|
|
|
name = app.get_name().lower()
|
2019-11-06 10:34:08 +00:00
|
|
|
if name is None:
|
2019-09-19 04:09:53 +01:00
|
|
|
# Likely an empty .desktop entry, skip it
|
|
|
|
continue
|
2017-04-04 19:04:14 +01:00
|
|
|
entry = [app]
|
|
|
|
tokenized_name = tokenizer.tokenize(name)[0]
|
|
|
|
|
|
|
|
if name in self.appmap:
|
|
|
|
self.appmap[name] += entry
|
|
|
|
else:
|
|
|
|
self.appmap[name] = entry
|
|
|
|
|
2019-11-06 10:34:08 +00:00
|
|
|
self.register_vocabulary(name, 'Application')
|
2017-04-04 19:04:14 +01:00
|
|
|
if name != tokenized_name:
|
2019-11-06 10:34:08 +00:00
|
|
|
self.register_vocabulary(tokenized_name, 'Application')
|
2017-04-04 19:04:14 +01:00
|
|
|
if tokenized_name in self.appmap:
|
|
|
|
self.appmap[tokenized_name] += entry
|
|
|
|
else:
|
|
|
|
self.appmap[tokenized_name] = entry
|
|
|
|
|
2019-11-06 10:34:08 +00:00
|
|
|
@intent_handler(IntentBuilder('LaunchDesktopApplicationIntent')
|
|
|
|
.require('LaunchKeyword')
|
|
|
|
.require('Application'))
|
2017-04-04 19:04:14 +01:00
|
|
|
def handle_launch_desktop_app(self, message):
|
2019-11-06 10:34:08 +00:00
|
|
|
"""Launch a dektop application using Desktop file."""
|
2017-04-04 19:04:14 +01:00
|
|
|
app_name = message.data.get('Application')
|
|
|
|
apps = self.appmap.get(app_name)
|
|
|
|
if apps and len(apps) > 0:
|
2019-11-06 10:34:08 +00:00
|
|
|
self.log.info('Launching {}'.format(app_name))
|
2017-04-04 19:04:14 +01:00
|
|
|
apps[0].launch()
|
2017-07-02 08:06:08 +01:00
|
|
|
|
2019-11-06 10:34:08 +00:00
|
|
|
@intent_handler(IntentBuilder('CloseDesktopApplicationIntent')
|
|
|
|
.require('CloseKeyword').require('Application'))
|
2017-06-25 19:42:30 +01:00
|
|
|
def handle_close_desktop_app(self, message):
|
2019-11-06 10:34:08 +00:00
|
|
|
"""Close application using killall -9."""
|
2017-06-25 19:42:30 +01:00
|
|
|
app_name = message.data.get('Application')
|
2019-11-06 10:34:08 +00:00
|
|
|
subprocess.call(['killall', '-9', app_name])
|
2017-04-04 19:04:14 +01:00
|
|
|
|
2019-11-06 10:34:08 +00:00
|
|
|
@intent_handler(IntentBuilder('LaunchWebsiteIntent')
|
|
|
|
.require('LaunchKeyword').require('Website'))
|
2017-04-04 19:04:14 +01:00
|
|
|
def handle_launch_website(self, message):
|
2019-11-06 10:34:08 +00:00
|
|
|
"""Open a website in the selected webbrowser."""
|
2017-04-04 19:04:14 +01:00
|
|
|
site = message.data.get("Website")
|
2018-03-05 12:22:48 +00:00
|
|
|
webbrowser.open(IFL_TEMPLATE % (quote(site)))
|
2017-04-04 19:04:14 +01:00
|
|
|
|
2019-11-06 10:34:08 +00:00
|
|
|
@intent_handler(IntentBuilder('SearchWebsiteIntent')
|
|
|
|
.require('SearchKeyword').require('Website')
|
|
|
|
.require('SearchTerms'))
|
2017-04-04 19:04:14 +01:00
|
|
|
def handle_search_website(self, message):
|
2019-11-06 10:34:08 +00:00
|
|
|
"""Open a webbrowser searching for query."""
|
|
|
|
site = message.data.get('Website')
|
|
|
|
search_terms = message.data.get('SearchTerms')
|
|
|
|
search_str = '{} {}'.format(site, search_terms)
|
2018-03-05 12:22:48 +00:00
|
|
|
webbrowser.open(IFL_TEMPLATE % (quote(search_str)))
|
2017-04-04 19:04:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
def create_skill():
|
|
|
|
return DesktopLauncherSkill()
|