Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli args #40

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Header.qml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ SilicaFlickable {

property alias searchFocus: placeHeader.searchFocus
property alias searchText: placeHeader.text
property alias searching: placeHeader.searching
hsjpekka marked this conversation as resolved.
Show resolved Hide resolved
property alias resultModel: placeHeader.resultModel

function searchResults(lst) {
placeHeader.searchResults(lst)
Expand Down
126 changes: 126 additions & 0 deletions src/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -517,4 +517,130 @@ ApplicationWindow
}
}
}

Component.onCompleted: {
var query = readCLIArguments(Qt.application.arguments,":");
var addr = "";

if ("parameters" in query) {
hsjpekka marked this conversation as resolved.
Show resolved Hide resolved
if ("address" in query.parameters)
addr = query.parameters.address;
}

startSearch(addr);
}

function readCLIArguments(argList, sepStr) {
// checks argList[] for "-q" = "--query", "--address"
// uses an url parser for 'query', but not for 'address'
var result = { scheme: "", action: "", comment: "", parameters: {} }
var nrArg = argList.length, dStr = "", i = 0, decode = true

if (sepStr === undefined)
sepStr = ":"

while(i < nrArg-1) {
if (argList[i] === "-q" || argList[i] === "--query") {
result = schemeComponents(argList[i+1], decode, sepStr)
i = nrArg
} else if (argList[i] === "--address") {
result.parameters.address = argList[i+1]
i = nrArg
}
i++
}

return result
}

function schemeComponents(url, decode, sepStr) {
// modified from www.sitepoint.com/get-url-parameters-with-javascript/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find any copyright notice from their article, but since you're citing the source, let's say it's fine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if there is a proper way to do it...

// url - string to be parsed
// decode - is decodeURIComponent() used for the parameter values?
// sepStr - separator between the scheme name and the query - ":" or "://" usually
var result = {}, queryParams = {}, j=0, schemeStr = url;

if (sepStr === undefined)
sepStr = ":"
if (decode === undefined)
decode = true

// if query string exists
if (schemeStr) {
j = schemeStr.indexOf(sepStr)
result.scheme = schemeStr.substring(0,j)

j += sepStr.length
schemeStr = schemeStr.substring(j) // from j to end

j = schemeStr.indexOf("?")
result.action = schemeStr.substring(0, j)

schemeStr = schemeStr.substring(j+1)

j = schemeStr.indexOf("#")
if (j >= 0) {
result.comment = schemeStr.substring(j+1)
schemeStr = schemeStr.substring(0, j)
}

// split our query string into its component parts
var arr = schemeStr.split('&');

for (var i = 0; i < arr.length; i++) {
// separate the keys and the values
var a = arr[i].split('=');

// set parameter name and value (use 'true' if empty)
var paramName = a[0];
var paramValue = typeof (a[1]) === 'undefined' ? true : a[1];
if (decode && typeof (paramValue) === typeof ("")) // if value is string
paramValue = decodeURIComponent(paramValue)

// if the paramName ends with square brackets, e.g. colors[] or colors[2]
if (paramName.match(/\[(\d+)?\]$/)) {

// create key if it doesn't exist
var key = paramName.replace(/\[(\d+)?\]/, '');
if (!queryParams[key]) queryParams[key] = [];

// if it's an indexed array e.g. colors[2]
if (paramName.match(/\[\d+\]$/)) {
// get the index value and add the entry at the appropriate position
var index = /\[(\d+)\]/.exec(paramName)[1];
queryParams[key][index] = paramValue;
} else {
// otherwise add the value to the end of the array
queryParams[key].push(paramValue);
}
} else {
// we're dealing with a string
if (!queryParams[paramName]) {
// if it doesn't exist, create property
queryParams[paramName] = paramValue;
} else if (queryParams[paramName] && typeof queryParams[paramName] === 'string'){
// if property does exist and it's a string, convert it to an array
queryParams[paramName] = [queryParams[paramName]];
queryParams[paramName].push(paramValue);
} else {
// otherwise add the property
queryParams[paramName].push(paramValue);
}
}
}
}
result.parameters = queryParams
return result;
}

function startSearch(address) {
if (address > "") {
header.searching = true;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have done this a bit differently, if you don't mind:
in PlaceHeader.qml, add a function:

function search(text) {
                searching = true
                resultList.model = undefined
                searchRequest(text)
}

and modify the EnterKey.onClicked handler in the PageHeader to call this function.

Then in Header.qml, add a forwarding function:

function search(text) {
  placeHeader.search(text)
}

and call it in the new startSearch() method of main you're introducing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I modified this a bit to write the address to the search field also.

    function search(text, changeText) {
        if (changeText)
            search.text = text // address from command line

header.resultModel = undefined;
header.searchText = address;
map.focus = true;
map.setSearchRequest(address);
}
return
}
}