Skip to content

Commit

Permalink
Add download search
Browse files Browse the repository at this point in the history
  • Loading branch information
deanishe committed Jan 7, 2020
1 parent 786b505 commit fc469d0
Show file tree
Hide file tree
Showing 8 changed files with 357 additions and 118 deletions.
31 changes: 31 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"os"
"os/exec"
"path/filepath"

aw "github.com/deanishe/awgo"
"github.com/deanishe/awgo/util"
Expand All @@ -24,6 +25,15 @@ var (
Exec: runHistory,
}

// search downloads
downloadsCmd = &ffcli.Command{
Name: "downloads",
Usage: "alfred-firefox -query <query> downloads",
ShortHelp: "search downloads",
LongHelp: wrap(`Search Firefox downloads.`),
Exec: runDownloads,
}

// search bookmarks
bookmarksCmd = &ffcli.Command{
Name: "bookmarks",
Expand Down Expand Up @@ -450,6 +460,27 @@ func runStatus(_ []string) error {
return nil
}

func runDownloads(_ []string) error {
log.Printf("searching downloads for %q ...", query)
downloads, err := mustClient().Downloads(query)
if err != nil {
return err
}

for _, dl := range downloads {
wf.NewItem(filepath.Base(dl.Path)).
Subtitle(util.PrettyPath(dl.Path)).
Arg(dl.Path).
UID(dl.Path).
Icon(&aw.Icon{Value: dl.Path, Type: aw.IconTypeFileIcon}).
Valid(true)
}

wf.WarnEmpty("Nothing Found", "Try a different query?")
wf.SendFeedback()
return nil
}

// run update check in background
func checkForUpdate() {
if wf.UpdateCheckDue() && !wf.IsRunning("update") {
Expand Down
57 changes: 49 additions & 8 deletions extension/alfred.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const Window = win => {

/**
* Tab object.
* @param {tabs.Tab} - Native tab object to create Tab from.
* @param {tabs.Tab} tab - Native tab object to create Tab from.
* @return {Object} - API Tab object.
*/
const Tab = tab => {
Expand All @@ -64,8 +64,7 @@ const Tab = tab => {

/**
* Bookmark object.
* @param {bookmarks.BookmarkTreeNode} - Native bookmark object to create
* Bookmark from.
* @param {bookmarks.BookmarkTreeNode} bm - Native object to create Bookmark from.
* @return {Object} - API Bookmark object.
*/
const Bookmark = bm => {
Expand All @@ -88,7 +87,7 @@ const Bookmark = bm => {

/**
* HistoryEntry object.
* @param {history.HistoryItem} - Native history object to create HistoryEntry from.
* @param {history.HistoryItem} hi - Native object to create HistoryEntry from.
* @return {Object} - API History object.
*/
const HistoryEntry = hi => {
Expand All @@ -106,6 +105,30 @@ const HistoryEntry = hi => {
return obj;
};

/**
* Download object.
* @param {downloads.DownloadItem} di - Native object to create Download from.
* @return {Object} - API Download object.
*/
const Download = di => {
let obj = {};
di = di || {};

obj.id = di.id || 0;
obj.path = di.filename || '';
obj.size = di.fileSize || 0;
obj.url = di.url || '';
obj.mime = di.mime || '';
obj.exists = di.exists || false;
obj.error = di.error || '';

obj.toString = function() {
return `#${this.id} "${this.path}" - ${this.url}`;
};

return obj;
};

/**
* Extension application object.
* @constructor
Expand Down Expand Up @@ -189,6 +212,9 @@ const Background = function() {
case 'search-history':
p = self.searchHistory(msg.params);
break;
case 'search-downloads':
p = self.searchDownloads(msg.params);
break;
case 'activate-tab':
p = self.activateTab(msg.params);
break;
Expand Down Expand Up @@ -360,15 +386,30 @@ const Background = function() {
* @return {Promies} - Resolves to array of History objects matching query.
*/
self.searchHistory = query => {
return browser.history
.search({ text: query, startTime: 0 })
.then(items => {
let history = items.filter(it => it.url).map(it => HistoryEntry(it));
return browser.history.search({ text: query, startTime: 0 }).then(items => {
let history = items.filter(it => it.url).map(it => HistoryEntry(it));
console.debug(`${history.length} history item(s) for "${query}"`);
return history;
});
};

/**
* Handle "search-downloads" command.
* @param {string} query - Search query.
* @return {Promise} - Resolves to array of Download objects matching query.
*/
self.searchDownloads = query => {
return browser.downloads
.search({
query: [query],
exists: true,
})
.then(items => {
console.debug(`${items.length} download(s) for "${query}"`);
return items.map(it => Download(it));
});
};

/**
* Handle "close-tabs-left" command.
* @param {number} tabId - ID of tab whose neighbours to close.
Expand Down
1 change: 1 addition & 0 deletions extension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"permissions": [
"<all_urls>",
"bookmarks",
"downloads",
"history",
"tabs",
"nativeMessaging"
Expand Down
Loading

0 comments on commit fc469d0

Please sign in to comment.