Skip to content

Commit

Permalink
Merge pull request #62 from nikwen/httpsFix
Browse files Browse the repository at this point in the history
Do not remove protocol when adding new forums (fixes issues with various https only forums)
  • Loading branch information
nikwen committed Mar 27, 2015
2 parents 466e68d + 62b792f commit 5500302
Showing 1 changed file with 47 additions and 14 deletions.
61 changes: 47 additions & 14 deletions ui/AddForumPage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,16 @@ Page {
text: docId !== "" ? i18n.tr("Edit") : i18n.tr("Add")
onTriggered: {
if (nameTextField.text !== "" && urlTextField.text !== "") {
//Remove protocol from url (so that there are no multiple instances of one forum but with different prefixes)
var url = urlTextField.text.trim()
var pos = url.indexOf("://")
if (pos === 4 || pos === 5) {
url = url.substring(pos + 3)
}

//Remove trailing slashes from url (see above for reasons)
while ((pos = url.lastIndexOf("/")) !== -1 && pos === url.length - 1) { //-1 check to catch urls which only consist of slashes
url = url.substring(0, url.length - 1)
}
//Clean up url (to make sure that there really are no multiple instances of the same forum)
url = cleanUpTrailingChars(url)

//Return if the url only consisted of slashes
if (url.trim() === "") {
//Used for comparison below (we do not want the same forum twice with different protocols)
var noProtocolUrl = removeProtocol(url)

//Return if the text field did not contain a full url
if (noProtocolUrl.trim() === "") {
notification.show(i18n.tr("Error: Url is invalid"))
return
}
Expand All @@ -67,9 +63,14 @@ Page {
if (contents["name"] === nameTextField.text) {
notification.show(i18n.tr("Error: Name already exists"))
return
} else if (contents["url"] === url) {
notification.show(i18n.tr("Error: Url already exists"))
return
} else if (contents["url"] !== undefined) {
var existingUrl = cleanUpTrailingChars(contents["url"])
existingUrl = removeProtocol(existingUrl)

if (existingUrl === noProtocolUrl) {
notification.show(i18n.tr("Error: Url already exists"))
return
}
}
}
}
Expand All @@ -93,6 +94,38 @@ Page {
}
}
}

function removeProtocol(url) {
var pos = url.indexOf("://")
if (pos === 4 || pos === 5) {
return url.substring(pos + 3)
} else {
return url
}
}

function cleanUpTrailingChars(url) {
var pos
var noChange = false

while (!noChange) {
noChange = true

//Remove trailing slashes from url
while ((pos = url.lastIndexOf("/")) !== -1 && pos === url.length - 1) { //unequal -1 check to catch urls which only consist of slashes
url = url.substring(0, url.length - 1)
noChange = false
}

//Remove sharps (#) from url
if ((pos = url.indexOf("#")) !== -1) {
url = url.substring(0, pos)
noChange = false
}
}

return url
}
}
]

Expand Down

0 comments on commit 5500302

Please sign in to comment.