Skip to content

add ability to search by string in address bar #197

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

Merged
merged 5 commits into from
Jan 27, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -682,9 +683,19 @@ public void go() {
}

public void navigateOrSearch() {
final String addressText = this.addressField.getText();
final String addressText = this.addressField.getText().trim();
final int periodIdx = addressText.indexOf('.');
final int spaceIdx = addressText.indexOf(' ');
final int aboutIdx = addressText.indexOf("about:");
if (addressText.charAt(0) == '?') {
this.search();
} else if ((spaceIdx != -1 || periodIdx == -1) && aboutIdx == -1) {
try {
final URL url = new URL("about:confirmSearch?" + addressText);
this.navigate(url);
} catch (MalformedURLException e) {
window.getTopFrame().alert("Malformed search URL.");
}
} else {
this.navigate(addressText, RequestType.ADDRESS_BAR);
}
Expand All @@ -693,11 +704,15 @@ public void navigateOrSearch() {
public void search() {
final ToolsSettings settings = ToolsSettings.getInstance();
final SearchEngine searchEngine = settings.getSelectedSearchEngine();
final String addressText = this.addressField.getText();
if (searchEngine != null) {
try {
final String addressText = this.addressField.getText();
assert (addressText.charAt(0) == '?');
this.navigate(searchEngine.getURL(addressText.substring(1)));
if (addressText.charAt(0) == '?') {
assert (addressText.charAt(0) == '?');
this.navigate(searchEngine.getURL(addressText.substring(1)));
} else {
this.navigate(searchEngine.getURL(addressText));
}
Copy link
Member

Choose a reason for hiding this comment

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

If I am not mistaken, the above changes are not required anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change prevents the Search Button on the browser from dropping the first character in the string; since the other way to call the search function is by starting the query with a question mark.

} catch (final java.net.MalformedURLException mfu) {
window.getTopFrame().alert("Malformed search URL.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.lobobrowser.primary.ext.BookmarkInfo;
import org.lobobrowser.primary.ext.BookmarksHistory;
import org.lobobrowser.primary.ext.HistoryEntry;
import org.lobobrowser.primary.settings.SearchEngine;
import org.lobobrowser.primary.settings.ToolsSettings;
import org.lobobrowser.util.Strings;
import org.lobobrowser.util.Timing;

Expand Down Expand Up @@ -110,6 +112,17 @@ private String getURLText(final java.net.URL url) {
return getSystemProperties();
} else if ("welcome".equals(path)) {
return getWelcomeMessage();
} else if ("confirmSearch".equals(path)) {
String query = url.getQuery();
if (query == null) {
query = "";
}
try {
final String searchQuery = java.net.URLDecoder.decode(query, "UTF-8");
return this.getSearchConfirmation(searchQuery);
} catch (final java.io.UnsupportedEncodingException uee) {
throw new IllegalStateException("not expected", uee);
}
} else {
return "<p>Unknown about path: " + path + "</p>" +
"<h3>Known paths are:</h3>" +
Expand All @@ -121,6 +134,50 @@ private String getURLText(final java.net.URL url) {
"</ul>";
}
}

private String getSearchConfirmation(final String searchQuery) {
final ToolsSettings settings = ToolsSettings.getInstance();
final Collection<SearchEngine> searchEngines = settings.getSearchEngines();
final StringWriter swriter = new StringWriter();
final PrintWriter writer = new PrintWriter(swriter);
writer.println("<html>");
writer.println("<head>Confirm Search</head>");
writer.println("<body>");
if (searchEngines.size() == 0) {
writer.println("No search engines were found.");
} else {
writer.println("<h3>Confirm external search by selecting a search engine:</h3>");
writer.println("<ol>");
for (final SearchEngine searchEngine : searchEngines) {
writeSearchEngineEntry(writer, searchEngine, searchQuery);
}
writer.println("</ol>");
}
writer.println("</body>");
writer.println("</html>");
writer.flush();
return swriter.toString();
}

private static void writeSearchEngineEntry(final PrintWriter writer, final SearchEngine searchEngine, final String searchQuery) {
final ToolsSettings settings = ToolsSettings.getInstance();
java.net.URL url = null;
try {
url = searchEngine.getURL(searchQuery);
} catch (final java.net.MalformedURLException mfu) {
mfu.printStackTrace();
}
final String urlText = "Search with " + searchEngine.getName();
writer.println("<LI>");
writer.println("<DIV>");
writer.println("<A href='" + url + "'>" + urlText + "</A>");
if (searchEngine.equals(settings.getSelectedSearchEngine())) {
writer.print("(Default)");
}
writer.println("<BR><BR>");
writer.println("</DIV>");
writer.println("</LI>");
}

private static String getWelcomeMessage() {
final Properties relProps = PlatformInit.getInstance().relProps;
Expand Down