Skip to content

Commit

Permalink
Support push imports from content hub
Browse files Browse the repository at this point in the history
It should support multiple imports, but I haven't had a chance to test
that.  The import dialog probably won't be that useful in that case,
especially if there are errors.

The import dialog generaly doesn't show as soon as it should, since the
import processing happens before it has a chance to draw the dialog on
the screen.

Refs #61.
  • Loading branch information
rschroll committed Apr 10, 2015
1 parent faab148 commit 1e318d2
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 3 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ if(CLICK_MODE)
set(EXEC "qmlscene -I ./ ui/main.qml --appargs=\"%f\"")
configure_file(manifest.json.in ${CMAKE_CURRENT_BINARY_DIR}/manifest.json)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/manifest.json DESTINATION ${DATA_DIR})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/beru.contenthub.json DESTINATION ${DATA_DIR})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/apparmor DESTINATION ${DATA_DIR})
else(CLICK_MODE)
set(DATA_DIR ${CMAKE_INSTALL_DATADIR}/${PROJECT_NAME})
Expand Down
2 changes: 1 addition & 1 deletion apparmor/beru.access.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"policy_groups": ["networking", "webview"],
"policy_groups": ["networking", "webview", "content_exchange"],
"policy_version": 1.2,
"read_path": ["@{HOME}/"]
}
2 changes: 1 addition & 1 deletion apparmor/beru.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"policy_groups": ["networking", "webview"],
"policy_groups": ["networking", "webview", "content_exchange"],
"policy_version": 1.2
}
3 changes: 3 additions & 0 deletions beru.contenthub.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"destination": ["ebooks", "documents"]
}
3 changes: 2 additions & 1 deletion manifest.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"hooks": {
"beru": {
"apparmor": "apparmor/beru@[email protected]",
"desktop": "beru.desktop"
"desktop": "beru.desktop",
"content-hub": "beru.contenthub.json"
}
},
"maintainer": "Robert Schroll <[email protected]>",
Expand Down
1 change: 1 addition & 0 deletions ui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set(UI_SRC_FILES
BrowserPage.qml
DefaultCover.qml
historystack.js
Importer.qml
LocalBooks.qml
main.qml
qmlmessaging.js
Expand Down
91 changes: 91 additions & 0 deletions ui/Importer.qml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* Copyright 2015 Robert Schroll
*
* This file is part of Beru and is distributed under the terms of
* the GPL. See the file COPYING for full details.
*/

import QtQuick 2.0
import Ubuntu.Components 1.1
import Ubuntu.Components.Popups 1.0
import Ubuntu.Content 0.1

import "components"

Item {
id: importer
property bool importError: false
property bool openImport: true

Connections {
target: ContentHub
onImportRequested: {
if (transfer.state === ContentTransfer.Charged)
importItems(transfer.items)
}
}

Reader {
id: importReader
}

function doImport(filename, item) {
return function () {
var components = filename.split("/").pop().split(".")
var ext = components.pop()
var dir = filesystem.getDataDir(localBooks.defaultdirname)
var basename =components.join(".")
var newfilename = basename + "." + ext
var i = 0
while (filesystem.exists(dir + "/" + newfilename)) {
i += 1
newfilename = basename + "(" + i + ")." + ext
}
item.move(dir, newfilename)
localBooks.addFile(dir + "/" + newfilename, true)
console.log("Importing as " + dir + "/" + newfilename)
if (openImport)
loadFile(dir + "/" + newfilename)
}
}

function importItems(items) {
var dialog = PopupUtils.open(importComponent)
openImport = (items.length == 1)
importError = false

for (var i=0; i<items.length; i++) {
var filename = items[i].url.toString().slice(7)
if (importReader.load(filename)) {
localBooks.inDatabase(
importReader.hash(),
function (currentfilename) {
if (openImport)
loadFile(currentfilename)
console.log("This was already imported as " + currentfilename)
},
doImport(filename, items[i]))
} else {
console.log("Import error: " + importReader.error)
importError = true
}
}
if (!importError)
PopupUtils.close(dialog)
}

Component {
id: importComponent
Dialog {
id: importDialog
title: importError ? i18n.tr("Error Importing File") : i18n.tr("Import in Progress")
text: importError ? importReader.error : ""
StyledButton {
text: i18n.tr("Dismiss")
onClicked: {
openImport = false
PopupUtils.close(importDialog)
}
}
}
}
}
11 changes: 11 additions & 0 deletions ui/LocalBooks.qml
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,17 @@ Page {
coverTimer.start()
}

function inDatabase(hash, existsCallback, newCallback) {
var db = openDatabase()
db.readTransaction(function (tx) {
var res = tx.executeSql("SELECT filename FROM LocalBooks WHERE hash == ?", [hash])
if (res.rows.length > 0 && filesystem.exists(res.rows.item(0).filename))
existsCallback(res.rows.item(0).filename)
else
newCallback()
})
}

function readBookDir() {
addBookDir()
listBooks()
Expand Down
7 changes: 7 additions & 0 deletions ui/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,15 @@ MainView {
id: server
}

Importer {
id: importer
}

function loadFile(filename) {
if (server.reader.load(filename)) {
while (pageStack.currentPage != localBooks)
pageStack.pop()

pageStack.push(bookPage, {url: "http://127.0.0.1:" + server.port})
window.title = server.reader.title()
localBooks.updateRead(filename)
Expand Down

0 comments on commit 1e318d2

Please sign in to comment.