Are File.isFile and File.isDirectory not working properly in Appcelerator Titanium? - titanium

I am working on an iPad app using appcelarator titanium and need to iterate over the content of a directory and determine the type of the contained items, whether it is a file or directory.
This is what I have so far:
dirFullPath = '/full/path/to/directory';
var dir = Titanium.Filesystem.getFile(dirFullPath);
var dirItems = dir.getDirectoryListing();
for ( var i=0; i<dir.length; i++ ) {
itemFullPath = dirFullPath
+ Titanium.Filesystem.getSeparator()
+ dir[i].toString();
testItem = Titanium.Filesystem.getFile(itemFullPath);
if ( testItem.exists() ) {
alert(itemFullPath + ' exists.'); // item exists, alert box appears
if ( testItem.isDirectory ) {
alert(itemFullPath + ' is a directory.'); // this code is never executed
}
else if ( testItem.isFile ) {
alert(itemFullPath + ' is a file.'); // this code is never executed
}
else {
alert(itemFullPath + ' is an unknown object.'); // this alert is always shown
}
}
}
I always get the alert box saying " is an unknown object.". It seems, that isFile and isDirectory are not working properly, or did I miss something? Does anybody else had the same problem?
Thanks for any advice!

The following should work:
var isDirectory = function(f){
return f.exists() && f.getDirectoryListing() != null;
}
var isFile = function(f){
return f.exists() && f.getDirectoryListing() == null;
}

Related

Using string translation in adapter return numbers

I am trying to use translation string in my adapter but it returns numbers instead
if(currentItem.budget != null){
holder.budget.text = "$ " + currentItem.budget.format()
} else {
holder.budget.text = R.string.open_to_suggestions.toString()
}
R.string.open_to_suggestions.toString() supposed to return string text Open to suggestions but it returns numbers such as 2131755113 not sure why! any idea?
To show the string resource you must use context.getString()
if(currentItem.budget != null) {
holder.budget.text = "$ " + currentItem.budget.format()
} else {
val context = holder.itemView.context
holder.budget.text = context.getString(R.string.open_to_suggestions)
}
Please take a look at the definition of getString here

Creating multiple mandatory fields in workflow

I have a requirement for multiple fields to be set whenever an issue is created.
I tried this
rule Mandatory
when <issue created or updated> {
Swimlane.required("Must have a swimlane");
UtgmsVehicleName.required("Must be attached to a vehicle ");
Subsystem.required("Subsystem must be set");
Assignee.required("Assignee must be set");
Fix versions.required("Fix versions must be set/");
}
What happens is that it continually asks for all the fields to be set. What is the best way to fulfil the requirement.
Based on Alex's suggestion i got this
rule MandatoryFields
when !isReported() {
var assigneeSet = Assignee != null;
var subSystemSet = Subsystem != null && Subsystem != {No subsystem};
var fixedVersionSet = Fix versions != null;
var assigneeValue = Assignee.oldValue;
var messageValue = "Mandatory fields:";
if (!assigneeSet) {
messageValue = messageValue + " Assignee";
}
if (!subSystemSet) {
messageValue = messageValue + " Subsystem";
}
if (!fixedVersionSet) {
messageValue = messageValue + " FixedVersion";
}
assert (assigneeSet && subSystemSet && fixedVersionSet): messageValue;
if (assigneeSet) {
Assignee = assigneeValue;
}
}
You can have something like
var noSwimlane = Swimlane == null;
var noSubsystem = Subsystem == null;
...
var message = "You need to fill the following fields: ";
if (noSwimlane) {
message += "Swimlane";
}
...
assert !(noSwimlane || noSubsystem || ...) : message;

Creating/Retrieving a Cart programmatically

For a module that I'm writing I need to retrieve a cart for a certain user (not necessary registered) after that a link is called and some data are passed.
My idea was to receive back a previously id passed that can help me to identify a certain cart.
My big problem is that I've search a lot for code cart creation into prestashop. Finally I've found something into
/* Cart already exists */
if ((int)$this->context->cookie->id_cart)
{
$cart = new Cart($this->context->cookie->id_cart);
if ($cart->OrderExists())
{
unset($this->context->cookie->id_cart, $cart, $this->context->cookie->checkedTOS);
$this->context->cookie->check_cgv = false;
}
/* Delete product of cart, if user can't make an order from his country */
elseif (intval(Configuration::get('PS_GEOLOCATION_ENABLED')) &&
!in_array(strtoupper($this->context->cookie->iso_code_country), explode(';', Configuration::get('PS_ALLOWED_COUNTRIES'))) &&
$cart->nbProducts() && intval(Configuration::get('PS_GEOLOCATION_NA_BEHAVIOR')) != -1 &&
!FrontController::isInWhitelistForGeolocation() &&
!in_array($_SERVER['SERVER_NAME'], array('localhost', '127.0.0.1')))
unset($this->context->cookie->id_cart, $cart);
// update cart values
elseif ($this->context->cookie->id_customer != $cart->id_customer || $this->context->cookie->id_lang != $cart->id_lang || $currency->id != $cart->id_currency)
{
if ($this->context->cookie->id_customer)
$cart->id_customer = (int)($this->context->cookie->id_customer);
$cart->id_lang = (int)($this->context->cookie->id_lang);
$cart->id_currency = (int)$currency->id;
$cart->update();
}
/* Select an address if not set */
if (isset($cart) && (!isset($cart->id_address_delivery) || $cart->id_address_delivery == 0 ||
!isset($cart->id_address_invoice) || $cart->id_address_invoice == 0) && $this->context->cookie->id_customer)
{
$to_update = false;
if (!isset($cart->id_address_delivery) || $cart->id_address_delivery == 0)
{
$to_update = true;
$cart->id_address_delivery = (int)Address::getFirstCustomerAddressId($cart->id_customer);
}
if (!isset($cart->id_address_invoice) || $cart->id_address_invoice == 0)
{
$to_update = true;
$cart->id_address_invoice = (int)Address::getFirstCustomerAddressId($cart->id_customer);
}
if ($to_update)
$cart->update();
}
}
if (!isset($cart) || !$cart->id)
{
$cart = new Cart();
$cart->id_lang = (int)($this->context->cookie->id_lang);
$cart->id_currency = (int)($this->context->cookie->id_currency);
$cart->id_guest = (int)($this->context->cookie->id_guest);
$cart->id_shop_group = (int)$this->context->shop->id_shop_group;
$cart->id_shop = $this->context->shop->id;
if ($this->context->cookie->id_customer)
{
$cart->id_customer = (int)($this->context->cookie->id_customer);
$cart->id_address_delivery = (int)(Address::getFirstCustomerAddressId($cart->id_customer));
$cart->id_address_invoice = $cart->id_address_delivery;
}
else
{
$cart->id_address_delivery = 0;
$cart->id_address_invoice = 0;
}
// Needed if the merchant want to give a free product to every visitors
$this->context->cart = $cart;
CartRule::autoAddToCart($this->context);
}
that is contained into FrontController.php (that seems to be called in every page). So, to me, a cart should always be present during a "user session".
But - yes, there's a but - when I try to retrieve a cart (in that way, into controller of my module)
$context=Context::getContext();
$id_cart=$context->cart->id;
$id_cart isn't there, so cart seems to miss. So I'm a little bit confused.
What's goin' on here? Someone could give me some pointers?
PS.:
I've tried to replicate that function (only the else part) but it doesn't work
You can force cart generation when the user isn't logged in and there is no product in the cart:
$context = Context::getContext();
if (!$context->cart->id) {
$context->cart->add();
$context->cookie->id_cart = $context->cart->id;
}
$id_cart = $context->cart->id;
Take a look at the processChangeProductInCart method in controllers/front/CartController.php
I'm using Prestashop 1.6, and #yenshiraks answer did not work for me. I cannot use $context->cart->add();, because $context->cartis null.
This is what worked in my case:
$context = Context::getContext();
$cart_id = null
if($context->cookie->id_cart) {
$cart = new Cart($context->cookie->id_cart);
$cart_id = $cart->id; // just in case the cookie contains an invalid cart_id
}
if(empty($cart_id)) {
$cart = new Cart();
$cart->id_lang = (int)$context->cookie->id_lang;
$cart->id_currency = (int)$context->cookie->id_currency;
$cart->id_guest = (int)$context->cookie->id_guest;
$cart->id_shop_group = (int)$context->shop->id_shop_group;
$cart->id_shop = $context->shop->id;
$cart->add();
$cart_id = $cart->id;
}
$context->cookie->id_cart = $cart_id;
To answer the question at the end: Even though a cart is always generated in the FrontController, it is not saved to the database, therefore the id is null.
If you are in a context, where the FrontController is instantiated (any page of the frontend, $context->cart->add(); will suffice to save an empty cart to the database.
If on the other hand, you are in a script, that is called directly (like prestashop/modules/my_module/script.php), you have to use the above code.

How to access Topic name from pdfs using poppler?

I am using poppler, and I want to access topic or headings of a particular page number using poppler, so please tell me how to do this using poppler.
Using the glib API. Don't know which API you want.
I'm pretty sure there is no topic/heading stored with a particular page.
You have to walk the index, if there is one.
Walk the index with backtracking. If you are lucky, each index node contains a PopplerActionGotoDest (check type!).
You can grab the title from the PopplerAction object (gchar *title) and get the page number from the included PopplerDest (int page_num).
page_num should be the first page of the section.
Assuming your PDF has an index containing PopplerActionGotoDest objects.
Then you simply walk it, checking for the page_num.
If page_num > searched_num, go back one step.
When you are at the correct parent, walk the childs. This should give you the best match.
I just made some code for it:
gchar* getTitle(PopplerIndexIter *iter, int num, PopplerIndexIter *last,PopplerDocument *doc)
{
int cur_num = 0;
int next;
PopplerAction * action;
PopplerDest * dest;
gchar * title = NULL;
PopplerIndexIter * last_tmp;
do
{
action = poppler_index_iter_get_action(iter);
if (action->type != POPPLER_ACTION_GOTO_DEST) {
printf("No GOTO_DEST!\n");
return NULL;
}
//get page number of current node
if (action->goto_dest.dest->type == POPPLER_DEST_NAMED) {
dest = poppler_document_find_dest (doc, action->goto_dest.dest->named_dest);
cur_num = dest->page_num;
poppler_dest_free(dest);
} else {
cur_num = action->goto_dest.dest->page_num;
}
//printf("cur_num: %d, %d\n",cur_num,num);
//free action, as we don't need it anymore
poppler_action_free(action);
//are there nodes following this one?
last_tmp = poppler_index_iter_copy(iter);
next = poppler_index_iter_next (iter);
//descend
if (!next || cur_num > num) {
if ((!next && cur_num < num) || cur_num == num) {
//descend current node
if (last) {
poppler_index_iter_free(last);
}
last = last_tmp;
}
//descend last node (backtracking)
if (last) {
/* Get the the action and do something with it */
PopplerIndexIter *child = poppler_index_iter_get_child (last);
gchar * tmp = NULL;
if (child) {
tmp = getTitle(child,num,last,doc);
poppler_index_iter_free (child);
} else {
action = poppler_index_iter_get_action(last);
if (action->type != POPPLER_ACTION_GOTO_DEST) {
tmp = NULL;
} else {
tmp = g_strdup (action->any.title);
}
poppler_action_free(action);
poppler_index_iter_free (last);
}
return tmp;
} else {
return NULL;
}
}
if (cur_num > num || (next && cur_num != 0)) {
// free last index_iter
if (last) {
poppler_index_iter_free(last);
}
last = last_tmp;
}
}
while (next);
return NULL;
}
getTitle gets called by:
for (i = 0; i < num_pages; i++) {
iter = poppler_index_iter_new (document);
title = getTitle(iter,i,NULL,document);
poppler_index_iter_free (iter);
if (title) {
printf("title of %d: %s\n",i, title);
g_free(title);
} else {
printf("%d: no title\n",i);
}
}

SWFAddress 2.2 isn't responding to specific URLs

I am implementing SWFAddress into a Flash movie, and while my navigation is setting the links correctly, when I type in a specific URL, it doesn't seem to communicate with the browser at all. Am I missing a listener or something?
http://client.deicreative.com/test/TBB/
This class talks to my navigation class:
import SWFAddress.as;
class code.RunSWFAddress {
public function RunSWFAddress(){
init();
}
private function init() {
var scope = this;
SWFAddress.setStrict(false);
SWFAddress.onChange = function() {
var value = SWFAddress.getValue();
var path = SWFAddress.getPath();
var id = SWFAddress.getParameter('id');
if (code.PageContent.getInstance().xmlVar1.getBytesLoaded() == code.PageContent.getInstance().xmlVar1.getBytesTotal()){
if(SWFAddress.getValue() == '/' || SWFAddress.getValue() == '') {
code.Navigation.getInstance().showPage(0);
} else {
for(var i:Number = 0; i<code.Startup.getInstance().numPages; i++){
if(SWFAddress.getValue() == code.Startup.getInstance().page_arr[i][0]){
code.Navigation.getInstance().showPage(i);
}
}
}
}
var title = 'The Broadway Building';
var names = SWFAddress.getPathNames();
for (var i = 0; i < names.length; i++) {
title += ' | ' + names[i].substr(0,1).toUpperCase() + names[i].substr(1);
}
var id = SWFAddress.getParameter('id');
if (id != '') {
title += ' | ' + id;
}
SWFAddress.setTitle(title);
}
}
}
I was missing the id attribute in my swfobject embed. Works now! :)