BlackBerry 10: Load GroupDataModel data into a JSON file? - blackberry-10

I have a ListView in QML using a cpp GroupDataModel that is created from a .json file in the assets folder. Items from this ListView are removed and added to. In cpp how do I get the GroupDataModel data into the JSON file?
I know there is this:
JsonDataAccess jda;
jda.save(huh?, "/app/native/assets/employees.json");
How do I get the GroupDataModel data into a QVariant to put in the first parameter of that function? I can't just stick my m_model GroupDataModel in there; it causes an error.

You have to iterate over your model with GroupDataModel::data() and GroupDataModel::childCount() to create your resulting QVariant, then store it. As far as I know, there's no automatic way to do this.
Edit: there is one.

for loading groupdatamodel content in to json file, you have to do:
QList<QVariantMap> myList = m_model->toListOfMaps();
QVariantList membersList;
foreach(QVariantMap s, myList){
membersList << s;
}
JsonDataAccess jda;
jda.save(membersList,path);

Related

How to add templates.xml using plugin?

Editor templates in eclipse can be imported from xml file. Instead of manually importing, wanted to create a plugin. Which will import the templates.xml kept in specified folder at start of the eclipse.
How this can be achieved?
You can use the JFace org.eclipse.jface.text.templates.persistence.TemplateReaderWriter to read a template.xml. Something like:
File file = .... file to read
TemplateReaderWriter reader = new TemplateReaderWriter();
InputStream input = new BufferedInputStream(new FileInputStream(file));
TemplatePersistenceData[] datas = reader.read(input, null);
(code to deal with errors and closing the input left out)
You can then put the data in a TemplateStore:
TemplateStore fTemplateStore = ... store to use
for (TemplatePersistenceData data: datas) {
fTemplateStore.add(data);
}
fTemplateStore.save();
The template store you use depends on which templates you are updating.
For the Java Editor template store you can get the store with
JavaPlugin.getDefault().getTemplateStore();
But the JavaPlugin is not part of the official Eclipse API.
The above code is a simplified version of the import code in org.eclipse.ui.texteditor.templates.TemplatePreferencePage

How to get the content of all files in a given directory?

Octokit allows me to easily retrieve the list of files in a given folder like so:
var directoryContent = await _githubClient.Repository.Content.GetAllContents("MyUsername", "MyProject", "TheFolderName").ConfigureAwait(false);
Unfortunately, the Content property for each file is null which means that I have to loop through the list of files and retrieve each one separately like so:
foreach (var file in directoryContent)
{
var fileWithContent = await _githubClient.Repository.Content.GetAllContents("MyUsername", "MyProject", file.Path).ConfigureAwait(false);
... do something with the content ...
}
Is there a more efficient way to get the content of all files in a given folder?
Just for reference: This is on purpose. If you read a whole directory and load all the content in one step it's huge load. So, the first call skips the content (Content property is null). The second step requests just this file and then the content is present.
Only thing I'm still missing is a method that can return just one result instead of a list if I pull a single file.

Check for Valid Image using getFilesAsync()

Using WinJS, while looping through a directory, how to retrieve only images in that particular directory and ignoring any other file extension, including the DoubleDots .. and the SingleDot . etc?
Something like:
var dir = Windows.Storage.KnownFolders.picturesLibrary;
dir.getFilesAsync().done(function (filesFound) {
for(var i=0; i < filesFound.length; i++){}
if(filesFound[i] IS_REALLY_AN_IMAGE_(jpeg,jpg,png,gif Only)){
//Retrieve it now!
}else{
//Escape it.
}
}})
Instead of trying to process pathnames, it will work much better to use a file query, which lets the file system do the search/filtering for you. A query also allows you to listen for the query's contentschanged event if you want to dynamically track the folder contents rather than explicitly enumerating again.
A query is created via StorageFolder.createFileQuery, createFolderQuery, or other variants. In your particular case, where you want to filter by file types, you can use createFileQueryWithOptions. This function takes a QueryOptions object which you can initialize with an array of file types. For example:
var picturesLibrary = Windows.Storage.KnownFolders.picturesLibrary;
var options = new Windows.Storage.Search.QueryOptions(
Windows.Storage.Search.CommonFileQuery.orderByName, [".jpg", ".jpeg", ".png", ".gif"]);
//Could also use orderByDate instead of orderByName
if (picturesLibrary.areQueryOptionsSupported(options)) {
var query = picturesLibrary.createFileQueryWithOptions(options);
showResults(query.getFilesAsync());
}
where showResults is some function that takes the promise from query.getFilesAsync and iterates as needed.
I go into this subject at length in Chapter 11 of my free ebook, Programming Windows Store Apps with HTML, CSS, and JavaScript, 2nd Edition, in the section "Folders and Folder Queries". Also refer to the Programmatic file search sample, as I do in the book.
When you want to display the image files, be sure to use thumbnails instead of loading the whole image (images are typically much larger than a display). That is, for each StorageFile, call its getThumbnailAsync or getScaledImageAsThumbnailAsync method. Pass the resulting thumbnail (blob) to URL.createObjectURL which returns a URL you can assign to an img.src attribute. Or you can use a WinJS.UI.ListView control, but that's another topic altogether (see Chapter 7 of my book).

How do I format HTML code in Code Mirror when the value is set to it?

I am using the Code Mirror plugin to edit HTML source of a page. The HTML code is fetched from database and set as value in Code Mirror. But, after setting the value it is displayed in the same format in which it was saved in the database. How can I display it in proper format? Thanks in advance.
There is a function called autoFormatRange
editor.autoFormatRange(range.from, range.to);
This fragment from the CodeMirror Group might be what you need:
function autoFormat() {
var totalLines = editor.lineCount();
var totalChars = editor.getTextArea().value.length;
editor.autoFormatRange({line:0, ch:0}, {line:totalLines, ch:totalChars});
}
http://codemirror.net/2/demo/formatting.html

Eclipse - Export/Save Search Results

Eclipse's Search results view is quite handy with its tree-like structure. Is there any way to export these results to a readable text format or save them to a file for later use?
I've tried using copy & paste but the resulting text format is far from readable.
You can change the mode from tree to list by click 'upper-right corner triangle' ->'show in list', then just copy all the files in the list , it will be a perfect list of search result
I'm using Eclipse Search CSV Export.
No I don't think there is a possibility to export the results yet. (Update: Now there's a suitable plugin available). But you should be able to use the eclipse search framework programmatically an export the entries by yourself.
I did not test the following snipped but implemeted a custom search that way once (using the RetrieverAction class). You should be able to listen to search result changes without the action as well:
TextSearchQueryProvider provider= TextSearchQueryProvider.getPreferred();
// your input (you'll have to implement that one I think...)
TextSearchInput input = new TextSearchQueryProvider.TextSearchInput();
ISearchQuery query= provider.createQuery(input);
ISearchResult result = query.getSearchResult();
result.addListener(new ISearchResultListener() {
public void searchResultChanged(SearchResultEvent e) {
// -> export result
}
});
// run the query
NewSearchUI.runQueryInBackground(query);
Again: I did not test that at all and don't know if there is a better approach as well..