QuetzalFieldsDialog::QuetzalFieldsDialog(const char *title, const char *primary,
										 const char *secondary, PurpleRequestFields *fields,
										 const char *ok_text, GCallback ok_cb,
										 const char *cancel_text, GCallback cancel_cb,
										 void *user_data, QWidget *parent)
											 : QuetzalRequestDialog(PURPLE_REQUEST_FIELDS, user_data, parent)
{
	m_ok_cb = (PurpleRequestFieldsCb) ok_cb;
	m_cancel_cb = (PurpleRequestFieldsCb) cancel_cb;
	m_fields = fields;
	DataItem root = createItem(title, primary, secondary);
	GList *group_it = purple_request_fields_get_groups(m_fields);
	bool useGroupBox = group_it->next;
	debug() << "useGroupBox" << useGroupBox;
	for (; group_it; group_it = group_it->next) {
		PurpleRequestFieldGroup *group = reinterpret_cast<PurpleRequestFieldGroup*>(group_it->data);
		DataItem groupItem;
		if (useGroupBox)
			groupItem = DataItem(purple_request_field_group_get_title(group));
		GList *field_it = purple_request_field_group_get_fields(group);
		for (; field_it; field_it = field_it->next) {
			PurpleRequestField *field = reinterpret_cast<PurpleRequestField*>(field_it->data);
			debug() << field->id << purple_request_field_is_visible(field)
					<< purple_request_field_get_type(field);
			if (!purple_request_field_is_visible(field))
				continue;
			DataItem item(purple_request_field_get_id(field),
						  purple_request_field_get_label(field),
						  QVariant());
			switch (purple_request_field_get_type(field)) {
			case PURPLE_REQUEST_FIELD_STRING:
				item.setData(QString::fromUtf8(purple_request_field_string_get_default_value(field)));
				item.setProperty("multiline", purple_request_field_string_is_multiline(field));
				item.setProperty("password", bool(purple_request_field_string_is_masked(field)));
				item.setReadOnly(!purple_request_field_string_is_editable(field));
				debug() << item.name() << item.data() << item.property("password") << item.isReadOnly();
				break;
			case PURPLE_REQUEST_FIELD_INTEGER:
				item.setData(purple_request_field_int_get_default_value(field));
				item.setProperty("minValue", G_MININT);
				item.setProperty("maxValue", G_MAXINT);
				break;
			case PURPLE_REQUEST_FIELD_BOOLEAN:
				item.setData(bool(purple_request_field_bool_get_default_value(field)));
				break;
			case PURPLE_REQUEST_FIELD_CHOICE:
				{
					GList *labels = purple_request_field_choice_get_labels(field);
					QStringList alternatives;
					for (; labels; labels = labels->next)
						alternatives << QString::fromUtf8(reinterpret_cast<char*>(labels->data));
					item.setData(alternatives.value(purple_request_field_choice_get_default_value(field)));
					item.setProperty("alternatives", alternatives);
				}
				break;
			case PURPLE_REQUEST_FIELD_LIST:
				break;
			case PURPLE_REQUEST_FIELD_LABEL:
				item.setData(purple_request_field_get_label(field));
				item.setReadOnly(true);
				item.setProperty("hideTitle", true);
				break;
			case PURPLE_REQUEST_FIELD_IMAGE:
				{
					QImageReader reader;
					QByteArray data = QByteArray::fromRawData(purple_request_field_image_get_buffer(field),
															  purple_request_field_image_get_size(field));
					QBuffer buffer(&data);
					reader.setDevice(&buffer);
					QSize imageSize = reader.size();
					if (imageSize.isValid()) {
						imageSize.setWidth(imageSize.width() * purple_request_field_image_get_scale_x(field));
						imageSize.setHeight(imageSize.height() * purple_request_field_image_get_scale_y(field));
						reader.setScaledSize(imageSize);
					}
					item.setData(qVariantFromValue(reader.read()));
					item.setProperty("imageSize", qVariantFromValue(imageSize));
					item.setReadOnly(true);
				}
				break;
			case PURPLE_REQUEST_FIELD_ACCOUNT:
				break;
			case PURPLE_REQUEST_FIELD_NONE:
			default:
				continue;
			}
			item.setProperty("mandatory", purple_request_field_is_required(field));
			if (useGroupBox)
				groupItem.addSubitem(item);
			else
				root.addSubitem(item);
		}
		if (useGroupBox)
			root.addSubitem(groupItem);
	}
	createItem(root, ok_text, cancel_text);
	return;
}
Beispiel #2
0
Request::FieldsDialog::FieldsDialog(const char *title, const char *primary,
                                    const char *secondary, PurpleRequestFields *request_fields,
                                    const char *ok_text, GCallback ok_cb, const char *cancel_text,
                                    GCallback cancel_cb, void *user_data)
    : RequestDialog(title, primary, secondary, ok_text, ok_cb, cancel_text,
                    cancel_cb, user_data), fields(request_fields)
{
    tree = new CppConsUI::TreeView(AUTOSIZE, AUTOSIZE);
    lbox->AppendWidget(*tree);

    bool grouping = true;
    GList *groups = purple_request_fields_get_groups(fields);
    if (!groups)
        return;
    if (!purple_request_field_group_get_title(
                reinterpret_cast<PurpleRequestFieldGroup*>(groups->data))
            && !groups->next)
        grouping = false;
    for (; groups; groups = groups->next) {
        PurpleRequestFieldGroup *group
            = reinterpret_cast<PurpleRequestFieldGroup*>(groups->data);

        CppConsUI::TreeView::NodeReference parent = tree->GetRootNode();
        if (grouping) {
            const char *title = purple_request_field_group_get_title(group);
            if (!title)
                title = _("Settings group");

            CppConsUI::TreeView::ToggleCollapseButton *button
                = new CppConsUI::TreeView::ToggleCollapseButton(title);
            parent = tree->AppendNode(tree->GetRootNode(), *button);
        }

        for (GList *gfields = purple_request_field_group_get_fields(group);
                gfields; gfields = gfields->next) {
            PurpleRequestField *field
                = reinterpret_cast<PurpleRequestField*>(gfields->data);

            if (!purple_request_field_is_visible(field))
                continue;

            PurpleRequestFieldType type = purple_request_field_get_type(field);

            switch (type) {
            case PURPLE_REQUEST_FIELD_STRING:
                tree->AppendNode(parent, *(new StringField(field)));
                break;
            case PURPLE_REQUEST_FIELD_INTEGER:
                tree->AppendNode(parent, *(new IntegerField(field)));
                break;
            case PURPLE_REQUEST_FIELD_BOOLEAN:
                tree->AppendNode(parent, *(new BooleanField(field)));
                break;
            case PURPLE_REQUEST_FIELD_CHOICE:
                tree->AppendNode(parent, *(new ChoiceField(field)));
                break;
            case PURPLE_REQUEST_FIELD_LIST:
                if (purple_request_field_list_get_multi_select(field))
                    tree->AppendNode(parent, *(new ListFieldMultiple(field)));
                else
                    tree->AppendNode(parent, *(new ListFieldSingle(field)));
                break;
            case PURPLE_REQUEST_FIELD_LABEL:
                tree->AppendNode(parent, *(new LabelField(field)));
                break;
            case PURPLE_REQUEST_FIELD_IMAGE:
                tree->AppendNode(parent, *(new ImageField(field)));
                break;
            case PURPLE_REQUEST_FIELD_ACCOUNT:
                tree->AppendNode(parent, *(new AccountField(field)));
                break;
            default:
                LOG->Error(_("Unimplemented Request field type."));
                break;
            }
        }
    }
}